PHP Session Variables Lost

Posted on March 26, 2008 by Tony Fendall.
Categories: Bugs/Fixes.

CookieAs I mentioned in my previous post, I recently upgraded my PHP installation to PHP 5, and the process caused me a lot of grief.  I’m trying to document my experiences here so that in the future other people will hopefully be saved the pain that I went through.

The Problem
Once I had my PHP scripts running, I quickly noticed was that every time I stored a value into the session object, that value would be gone by the time the next next request to the script came through.

I did a lot of tests to eliminate any possibility that there was nothing else in the system which might be clearing the session variables or overwriting them etc. Eventually I tracked it down to the fact the php.ini file contained a path where session data would be stored, and similar to the previous issue I had with PHP, it was again DOS style file paths which were causing the problem.

The Solution
One you have identified the problem, the fix is fairly easy.

  • Open up your php.ini file and find the session.save_path variable. (This variable was on line 594 of my PHP 5.2.1 php.ini file)
  • Change this path to be the full path to the folder where you want the session data to be stored (not the truncated DOS path).
  • Make sure that the folder you specified exists in your file system because PHP can’t create it.
  • Restart your web site under IIS to make sure the new values are loaded.

While You’re at It
While you have your php.ini file open, you may as well also fix the temporary upload directory entry as well. The variable you are looking for is upload_tmp_dir, and in my php.ini file this is on the line above the session.save_path variable.

Like session.save_path, this variable needs to be the full path to the directory where PHP should temporarily store uploaded files, and the directory needs to exist on the file system because PHP cannot create it.

Greg Farnham
Comment on April 30th, 2008.

I’ve been looking for a solution for this problem for about a week now.

I cleared my temp files and deleted said folder.
recreating the folder (even with the truncated name) solved my problem.

Hamy
Comment on June 20th, 2008.

Nice! Thanks

Comment on February 2nd, 2009.

Wow, I’ve been looking for a solution to this problem for a couple weeks now. It’s been driving me crazy! Finally I typed the right description into Google and happened on your post. Thank you so much!