Simply: as an anonymous user I can't save data to $_SESSION and then retrieve it on other pages. As a logged in user, this works as expected.

Overview:
- new Drupal v5.3 install. No modules installed other than defaults.
- PHP v4.3.1.0
- logged out (i.e., viewing as an anonymous user)
- I set a $_SESSION value on one page
- I attempt to retrieve it on another page, but it is empty

I've attached code for a test module below. Once installed, navigate to the path 'sessinit' which should set the $_SESSION value. Then view any page to view the contents of the $_SESSION value and it shows up as empty for me. I would have expected to see 'mytestvalue'.

<?php
define
('KEY', 'mykey');

/* implementation of hook_menu */
function testsess_menu($may_cache)
{
   
$items = array();
   
    if (
$may_cache) {
    }
    else {
       
$items[] = array(
           
'type' => MENU_CALLBACK,
           
'path' => 'sessinit',
           
'callback' => '_testsess_init',
           
'access' => true,
        );
    }
    return
$items;
}

/* implementation of hook_init */
function testsess_init()
{
   
drupal_set_message("module init. session key is " . $_SESSION[KEY]);
}

function
_testsess_init()
{
   
$_SESSION[KEY] = 'mytestvalue';
   
drupal_set_message("initialized session key to: " . $_SESSION[KEY]);
    print
theme('page', 'session was initialized');
}
?>

I thought this might be an issue with using hook_init so I tried hook_nodeapi (with op='view') but even then when viewing a node the $_SESSION value was still empty.

Of course this all works as a logged-in user, just not as an anonymous one.

I'm going crazy attempting to do something that should be really simple. Since I've been working non-stop for a long, long time now, I have to assume I'm doing something really stupid that's right in front of me which I can't see.

Kendall
Abandoned Industry! :: http://invisiblethreads.com/galleries ::

Comments

mooffie’s picture

Your code works for me for both anonymous and logged-in users.

(Perhaps your pages are cached?)

kpander’s picture

Really? Sigh.

It's not caching -- caching is disabled *and* I've cleared out the cache tables anyways to make sure. I'm really at a loss here. This should *just work*. Argh.

Time to start testing on different servers I guess and see where this leads...

Kendall
Abandoned Industry! :: http://invisiblethreads.com/galleries ::

Kendall Anderson
Abandoned Industry | www.invisiblethreads.com

mooffie’s picture

I suggest you examine, using phpMyAdmin for example, your {sessions} table. After you locate the row for your session, check the 'session' column. If you see mykey|s:11:"mytestvalue"; there, then you know that half of the story is OK: that the session data is saved (the other half of the story is loading this session data).

kpander’s picture

The odd thing here is that the 'mykey:s:11...' is getting stored in the {sessions} table immediately after I set it. However, as soon as I navigate to any other page that session data is immediately wiped from the {sessions} record. The session id is correct (and matched to the browser's PHPSESSID cookie). And the session id isn't changing between page loads (as it shouldn't).

I guess I'll have to poke around Drupal's internals to see if I can get any further...

Kendall
Abandoned Industry! :: http://invisiblethreads.com/galleries ::

Kendall Anderson
Abandoned Industry | www.invisiblethreads.com

kpander’s picture

To anyone else who comes across this... The problem was I was missing user uid=0 in the {users} table. Putting user 0 back in (as well as an role entry for that user) solved it.

Somewhat related comment here:
http://drupal.org/node/21120#comment-48394

sess_read() was trying to do a join with the users table and since there was no user with uid=0, it returned no results for the session.

Kendall
Abandoned Industry! :: http://invisiblethreads.com/galleries ::

Kendall Anderson
Abandoned Industry | www.invisiblethreads.com

mooffie’s picture

[EDIT: Comment deleted. I discovered this UID 0 row issue after a look at sess_read(), and wrote about it here, but you already found the answer by that time ;-) ]