PHP Session Data Not Being Saved

PHP Session variable is not saving

Not sure what your 3rd page looks like but mine would be:

session_start(); // Use session variable on this page. This function must put on the top of page.

if( isset($_SESSION['i_am_in']) ) echo "logged in";

My Page 2:

if ($login_ok) {
session_start(); // Create session & settings
$_SESSION['abc123'] = 'whatever';
header('Location: page3.php');
}

PHP Session not Saving

Your session directory (probably /tmp/) is not writable.

Check with session_save_path() if it is writable.

if (!is_writable(session_save_path())) {
echo 'Session path "'.session_save_path().'" is not writable for PHP!';
}

PHP Session Data Not Being Stored

Here are a couple suggestions (I don't really know what's happening and/or why ; so they are only suggestions ; maybe one will solve the problem ^^ ).

First of all, a couple of questions :

(They matter at least if none of these suggestion does the trick)

  • Which version of PHP / Apache are you using ?
  • Are you on Windows ? Linux ?
  • If you are on your "production" server, what hosting service are you using ? Maybe there's something special about it ?
  • Is the problem present for every one ?

    • Is there always a problem when you are browsing the site ?
    • Is it still present when you are accessing the site from another browser ?
    • What about from another computer ?
  • If you use something like var_dump($_SESSION); die; at the end of the script that sets data in session, what does it give ?


First idea : what if you set some header to disable caching by the browser ?

Stuff like this, for instance :

session_start();
header("Cache-control: private");



Second idea (at least if you are on windows) : did you try disabling you antivirus / firewall ?

Is the session cookie correctly created in the client's browser ?

If you are using sub-domains (or not) : is the cookie's domain OK ? What about it's expiration date ?



Third idea :

  • you said error_reporting is set to E_ALL, which is nice
  • what about display_errors ? Is it set to On so that errors get displayed ?
  • Is there anything interesting in PHP/Apache's error_log ?



Another one : Are you sure there is absolutly nothing that gets to the output before the session_start ? Not even white spaces ?



Yet another one : Are you sure about permissions on the directories / files ?

  • Permission to write in a directory means you can create new files, and/or delete old ones.

    • But, if I remember correctly, not that you can modify them
  • To modify files, you need write access on the files too

    • Actually, your webserver need write access to those files ^^

What are the permissions on the session's directory, and on the (empty) files that get created ?



I'm beginning to run out of ideas... With a bit of luck, maybe one of those will be the right one... Or help you find out what the right one would be !

Good luck !

PHP Session data not being saved

Thanks for all the helpful info. It turns out that my host changed servers and started using a different session save path other than /var/php_sessions which didn't exist anymore. A solution would have been to declare ini_set(' session.save_path','SOME WRITABLE PATH'); in all my script files but that would have been a pain. I talked with the host and they explicitly set the session path to a real path that did exist. Hope this helps anyone having session path troubles.

PHP Session not being saved

Many thanks to Ryan Vincent who helped solve this over chat.

It stems from the way the actual scripts were loaded - using relative paths from a file higher up the directory. Unfortunately, this caused PHP some issues but didn't generate any errors. By transitioning to absolute paths: DIR . api/filename.php we managed to fix the problem.

Session Variables Not Being Stored

If you want your session variables to appear on any other page, then session_start(); must be included inside all your pages using sessions.


Footnotes:

Should you happen in the future to get the following error message: headers already sent, you could add ob_start(); just above session_start();

Relevant links:

  • http://www.php.net/manual/en/features.sessions.php
  • http://www.php.net/session_start
  • http://www.php.net/ob_start
  • http://www.php.net/header


Related Topics



Leave a reply



Submit