Warning: Cannot modify header information error

This is the error i recieve when I use the log in system of my website. The session ID is correctly sent to the database and the form is processed.
Although, when it comes to redirecting to the logged in homepage this error appears. I have tested this exact script with no problems in my computers local host WAMP server.
When uploaded online and tested the error appears. Please help!:
Warning: Cannot modify header information - headers already sent by (output started at /home/gr/public_html/members_login_handle.php:6) in /home/gr/public_html/members_login_handle.php on line 97.

The coding for the members_login_handle.php which processes my members_login.php form to login is below:

[php]<?php
session_start();
?>

Staff Login
img
Great Linford
Primary School
Be proud to do the "Right Thing"

img

<?php include_once('members_login_con.php'); $id = $_REQUEST['id']; $pwd = md5($_REQUEST['pwd']); if(preg_replace('/[a-zA-Z0-9]/', '', $id)){ echo "Invalid entry"; echo "
"; echo "
Please try again
."; } else{ $check=mysql_query("SELECT * FROM users WHERE email = '".$id."' AND pwd = '".$pwd."'"); $row_num=mysql_num_rows($check); if($row_num < 1){ echo "The user name and/or password has been entered incorrectly."; echo "
"; echo "
"; echo "Please Try again."; }else{ $_SESSION['members'] = md5($_REQUEST['number']); $members = $_SESSION['members']; $_SESSION['id']=$id; $check = mysql_query("SELECT*FROM $table WHERE id= '".$id."' AND pwd = '".$pwd."'"); $row_num = mysql_num_rows($check); if($row_num < 1){ mysql_query("INSERT INTO $table (session,id,pwd) VALUES ('$members','$id','$pwd')"); header('location:"members_area.php?sess='.$URLsession.'"'); }else{ echo "You are not authorised to log in concurrent sessions"; echo "
"; echo "Try again"; } } } ?>[/php]

Thanks and can’t wait to hear back with responses. Been trying to fix it for days…
If any additional infomation is required I would be happy to provide it.
Louise

If any information has already been sent to the browser (HTML code or even whitespace outside of PHP brackets), then the header() function is not usable. For redirecting you can try my custom function below:

[php]/**

  • Redirects to the page specified in $location
  • @param string $location Where to redirect to
  • @param integer $delay Delay in seconds
    */
    function redirect($location,$delay=0)
    {
    if(headers_sent() === true && $delay <= 0)
    {
    header(“Location: “.$location);
    }
    else
    {
    echo $delay == 0 ? ‘’
    : ‘’;
    echo ‘’;
    }
    }[/php]

make it easy on yourself always use metarefresh :smiley: that has made life so much more simple for me, and I never recieve any complaints about from any of clients about redirects not work and you will never get a header sent error EVER.
here is the the simple code of it below, you can change the number 2 to whatever you want, that is how many seconds until redirect.
[php]

[/php] dont forget if you ever use echo and put it in quotes, remove all the quotes out of it like so: [php] echo"";

[/php]

Plintu, meta refresh requires the page to be loaded and therefore creates a slower refresh even with the delay parameter set to 0 as headers have to be sent. If you are able to use PHP header() to redirect, it is faster and easier on the client plus it allows setting it to a permanent (301) redirect (more SEO friendly).

I guess I kind of got myself stuck in my old ways on all the sites I do I set them all up the same way ,which sends headers already so rather than change my coding style I stick with what works for me :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service