2

Possible Duplicate:
Headers already sent by PHP

I am making a simple login page however my $_SESSION variables are not transferring between pages. I've read numerous other posts about session_start() at the beginning of each page, folder writing priveleges, session_write_close(), etc., but none have worked. The session_write_close() doesn't make a difference withor without so I just left it in. All of the code below works fine as I have left out code below and above such as where $login_fail comes from.

Currently I set the the $_SESSION variables as shown in the code below:

if($login_fail == "")
{
    $query = "SELECT first_name, last_name, email_address,password FROM user_info WHERE     email_address = '$email_address' AND password = '$password'";
    $result = mysql_query($query);

    if(!$result) die ("Database access : " .mysql_error());
    elseif (mysql_num_rows($result))
    {
        $row = mysql_fetch_row($result);
        $token = "$email_address";
        $token1 = "$password";
        echo "token: $token, token1: $token1, row[1]: $row[1], row2: $row[2] </ br>";
        if($token == $row[2] && $token1 == $row[3]) 
        {
            session_start();
            $_SESSION['first_name'] = $row[0];
            $_SESSION['last_name'] = $row[1];
            $_SESSION['email_address'] = $row[2];
            $_SESSION['password'] = $row[3];
            $_SESSION['check'] = md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']);
            //print_r($_SESSION);
            session_write_close();
            header("Location: http://127.0.0.1/websiteproject/test.php"); 
        }
        else die("Invalid username/password combination");       
    }
}
else
{
    echo "Login failed. Please try again.";
}

I have done print_r($_SESSION) and it prints all the correct information.

The session variables are then called again in my test.php just to see what happens in really simply code.

session_start();
print_r($_SESSION);

The result is an empty array. When I go to the easyphp temp file where my sessions are written I always find two files: the original one with all the correct information and a new one with no information. It seems as if when I call the second session_start() it is literally starting a new session and not recalling the current session.

I try to do my research to give as much info as possible and not waist people's time. Any ideas are greatly appreciated. Odd is that this was working a few days ago and then I started making changes to files deeper into the program and this happened. So I made the test.php just to find out more about the transfer problems.

Community
  • 1
  • 1
jlofft
  • 33
  • 1
  • 5
  • **Always** enable [error reporting](http://php.net/manual/en/function.error-reporting.php)! – PeeHaa Jul 07 '12 at 19:43

1 Answers1

7

You cannot call session_start() once any data or whitespace has been output to the browser.

In the code you posted, you are calling echo "token: $token, token1: $token1, row[1]: $row[1], row2: $row[2] "; prior to calling session_start() which will not start the session.

Add error_reporting(E_ALL); ini_set('display_errors', 1); to the beginning of your script and you should see a warning about not being able to start the session because output has already been sent.

Try moving the session_start(); call to the very beginning of the file.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • I made the suggested changes however it is still doing the same thing. I moved session_start() to the very first line of code and removed all echo statements. The issue is I never had trouble starting the session as I can open the session from my Easy PHP temp folder and see all the correct information. On the next page, in test.php, when I start the session it starts a new session with no information, almost as if it ignores the previously started session – jlofft Jul 07 '12 at 20:11
  • Use a tool like [Wireshark](http://www.wireshark.org/) or a firefox plugin [livehttpheaders](https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/) to view the HTTP requests and responses. My next guess is that the cookie domain for the original session is different than the one on the subsequent request, perhaps due to the `header()` redirect to `127.0.0.1`, maybe this needs to be `localhost` instead? – drew010 Jul 07 '12 at 20:14
  • GENIUS! Changing from 127... to localhost worked. Intersting it interprets 127... differently from localhost. Thanks a lot this has been bugging me for some time. Was making good progress on this project and then BAM! nothing. Also, another note. Having the echos and outputs didnt seem to change the ability for session_start() to start. I put them back in just for sanity because the echo "token..." has been there forever. Im using php 5.3.9, dont know if that makes a difference. Anyhow. Again thanks a lot, greatly appreciated. – jlofft Jul 07 '12 at 20:31
  • Output buffering may have saved you on the echo's possible, but usually it would prevent the session cookie from being sent. And yes about 127 vs localhost, it makes all the difference in the world when the browser looks at what host/ip you are on. It can't map an IP address to a hostname, and won't check a hostname's IP address for corresponding cookies. It has to be set to exactly what you have in your address bar, or `.host.com` to send cookies for all hosts on host.com. – drew010 Jul 07 '12 at 20:34