PHP Sessions Not Working

As a jack of all trades, I was doing some development work in PHP on one of my internal servers. As with most web sites, I needed to use sessions variables in the PHP script. However, when I attempted to create the session variable through code similar to below, the session variables were empty when I tried to work with them in the same page or another page.

<?php
  session_start();
  $_SESSION['user'] = "aaron";
  echo $_SESSION['user'];
?>

I searched the web for an answer and got the standard realm of how to resolve PHP sessions issues, such as ensuring “session_start()” is used BEFORE any HTML output is produced – none of the solutions worked. After thinking about the process involved with a session object, I started to wonder where the PHP process kept the sessions on the server. Some research on the Internet pointed to a temp folder, and on Linux with Apache this would probably be the /tmp directory. Unfortunately, my server is a Windows Server with IIS.

So, as one might imagine, the configuration of temp file location is kept in the php.ini file – which by the way, must reside in the C:\[WINDOWS FOLDER]\ and all changes require an IIS restart.

There are two steps to repair: (a) analyze and modify the php.ini file; and (b) modify the permissions on the folder as configured in the php.ini.

(a): In the php.ini file, locate the following configuration lines and replace with a folder of your choosing. In my situation the original location was the temp folder of the Administrator profile. Because I do not want to modify the permissions for that folder, I decided to create C:\Temp\php\session\ and C:\Temp\php\upload\. My php.ini configuration is similar to the following:

upload_tmp_dir="C:\Temp\php\upload"
session.save_path="C:\Temp\php\session"

(b): Create the new temp location as configured in the php.ini file. Modify the permissions and add IUSR_[name] with Write/Read/Execute. Although I would highly discourage you from doing so, in my case, since this is a test box I gave EVERYONE permission to the temp folder.

Restart IIS using the Services MMC or through the command prompt iisreset tool. PHP sessions should now work.

-Aaron Gilbert

About aaron

IT is not just a job but also a passion. Everything I have accomplished, both personally and professionally, has been generally entertaining, bordering on fun. Some of my projects, such as working with SharePoint Services workflow actions in Visual Studio or building a custom iSCSI SAN using the OpenSolaris, ZFS and COMSTAR, has been quite rewarding. You may think nerd...I think developing a new trend!