I stumbled across this when trying to use drupal_set_message() in hook_exit() in the course of some testing. The message is being correctly stored in $_SESSION, but the session doesn't persist beyond the end of the page request.

This happens after both types of cached respone (i.e. 200 or 304).

If the page is served via a non-cached response then the message is correctly displayed on the next pageview.

I guess the problem is that for cached pageviews the session never gets initialized.

Comments

gpk’s picture

I imagine the same problem will arise in hook_book() after a cached response.

Related: #201122: Drupal should support disabling anonymous sessions.

gpk’s picture

Title: Regression: data stored in $_SESSION in hook_exit() after a cached page response is lost » Regression: data stored in $_SESSION in hook_boot() or hook_exit() during a cached page response is lost

Workaround: prior to storing something in $_SESSION or calling drupal_set_message(), do

drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
drupal_session_start();
c960657’s picture

gpk’s picture

Um, this *is* #592482 ... ;)

c960657’s picture

Ah, so that's why they are so similar :-)

I meant #758730: Fatal error on logout.

gpk’s picture

From looking at the other issue (without testing anything) I think they may be related but different .. the other one seems specifically concerned with what happens after logout, whereas this is concerned with hook_boot/exit during a cached page response.

Sifro’s picture

I'm struggling with this error too.

I have a custom module implementing hook_boot:

function_mymodule_boot(){
  if(!isset($_SESSION['lock'])) {
    echo 'debug';
    $_SESSION['lock'] = true;
  }
}

As you can imagine, the "debug" gets echoed on every single page request, while it should be echoed only on the very first page that the user requests.
It happens both with caching ON and OFF.

If I try to put a var_dump($_SESSION['lock']) at the top of the function, i see a NULL being printed at every request.

@gpk your workaround works fine also with caching enabled, even without including the drupal_bootstrap() call... kind of a mistery. Have you been able to make any progresses on this in the meanwhile?

EDIT: no i was wrong, it doesn't work with caching enabled. THe first time I enter the site it's fine, but if i close the window and re-enter (and caching is enabled) I get a Fatal error: Call to undefined function drupal_session_start().

The drupal_bootstrap() call in hook_boot() gives an error too (regardless of caching setting):
Fatal error: Call to undefined function menu_execute_active_handler()

Sifro’s picture

gpk’s picture

Issue summary: View changes
Status: Closed (works as designed) » Active

I still regard this as an undocumented regression.

@7,

EDIT: no i was wrong, it doesn't work with caching enabled

That's why you need the

drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);

call since (amongst other things) it does an include on the necessary session.inc file.