views:

2605

answers:

2

Drupal employs a custom session handler that changes the familiar...:

$_SESSION['foo'] = 'bar';
echo $_SESSION['foo'];

...behavior. The above session variable of "foo" would not persist from page to page.

Many comments and forum entries at Drupal.com raise the issue that Drupal uses a custom session handler for performance reasons and server clustering support. However, I'm failing to find specific examples answering the question - "Need to manage your own session variables across pages in Drupal and don't want to resort to cookies? Here's how you do it..."

Does anyone have experience with this? My usage would NOT be within external Drupal pages but within template pages themselves. This is for an anonymous user, not a logged in one. $_SESSION behavior is as expected in that case.

+4  A: 

Ok, here's the answer - $_SESSION works for authenticated users as well as anonymous users as expected - if there are no other problems!

Thanks to some insight from this blog, I discovered that I had the same problem; my Drupal "users" table (or in my case "drupal_users") was missing a user with a UID of zero ("0").

Drupal uses UID 0 to manage sessions in a database via its custom session handler. If that user doesn't exist in the table (it should be there by default installation), then Drupal cannot attach session information to the anonymous user.

A: 

You sir, are a god. I deleted the user with UID of 0 because I thought it was a bug. Big mistake.

I added a new user with a UID of 0 and now my sessions are working properly.

Oh you poor soul! You know ALL TOO WELL what I went through to get to the bottom of that rabbit hole!