PHP and Sessions

I don’t know how many people have had this particular problem, but I know I’m not the only one. Two weeks ago I encountered a strange issue when using session_start() with PHP; the method call was not setting/creating a cookie, so I couldn’t access my session variables. I went to php.net and found several answers. I’m pretty sure this only applies to people who have are running this on their own server; ergo, hosting companies won’t have (or have already remedied) this issue.

I use PHP ver. 4.4.1 so I don’t know if this applies to versions 5+, but if you encounter the same problem, try one (or all) of the following:

  • Check to see if your php.ini file has a slash at the end of the session.cookie_path setting (e.g., “session.cookie_path = /”). Remove it so that it becomes “session.cookie_path =”. Apparently, the slash is read as an escape character, which would, obviously, cause issues.
  • Make sure that session_start() is the very first command in your document; above <html> and the <doctype>. This may seem obvious, but not an unlikely mistake.
  • You may need to implement a compact privacy policy, more info here: P3P, Cookies and IE6.0: A Case Study.

If you’ve been having the same problem and none of the above suggestions have helped, try looking for a solution at www.php.net, if that doesn’t work for you, well, you can always Google it.

Resource: http://us2.php.net/manual/en/ref.session.php