Anonymous users and $_SESSION.

Last modified: September 6, 2009 - 02:24

While testing our main web site I noticed that anonymous users were not able to store and save values in $_SESSION. Running down the path of what could be going wrong I called functions such as session_id(), and debug_print_backtrace() to see what might be causing this anomaly. I could not find anything relevant and then decided to do a few searches in the forums. I came across a few posts that seemed like they might be a solution but one finally caught my eye:

http://drupal.org/node/192165#comment-630167

Now this post mentioned that the user whose uid was zero is missing from the database and that restoring it was the answer to the issue. Opening a terminal and logging into our database I executed SELECT * FROM users WHERE uid=0;, the results returned zero rows for the query. Thinking back to when Drupal was first installed, I found a user with a uid of zero and though that this user with uid set to zero, no user name and password was a bug from installation and deleted the row.

If $_SESSION is not being read from and written to as an anonymous user from page to page, take a look at the users table in the database. Chances are that you can save yourself a lot of debugging time by making sure that a user of uid set to zero exists. If there isn't one in the database there is a simple fix for this issue, execute the following SQL command: INSERT INTO users (uid) VALUES (0);

Clear your cache and then go back to the pages in question and see if it makes any differences in what values are stored in $_SESSION as an anonymous user.

Another tip for User 0

This was more my problem, since I had a user 0:
http://drupal.org/node/205933

 
 

Drupal is a registered trademark of Dries Buytaert.