Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
gpk CreditAttribution: gpk commentedI imagine the same problem will arise in hook_book() after a cached response.
Related: #201122: Drupal should support disabling anonymous sessions.
Comment #2
gpk CreditAttribution: gpk commentedWorkaround: prior to storing something in $_SESSION or calling drupal_set_message(), do
Comment #3
c960657 CreditAttribution: c960657 commentedI wonder if this is a duplicate of #592482: Regression: data stored in $_SESSION in hook_boot() or hook_exit() during a cached page response is lost ...
Comment #4
gpk CreditAttribution: gpk commentedUm, this *is* #592482 ... ;)
Comment #5
c960657 CreditAttribution: c960657 commentedAh, so that's why they are so similar :-)
I meant #758730: Fatal error on logout.
Comment #6
gpk CreditAttribution: gpk commentedFrom 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.
Comment #7
Sifro CreditAttribution: Sifro commentedI'm struggling with this error too.
I have a custom module implementing hook_boot:
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()
Comment #8
Sifro CreditAttribution: Sifro commentedhttp://drupal.stackexchange.com/questions/88905/custom-php-code-before-a...
Comment #9
gpk CreditAttribution: gpk commentedI still regard this as an undocumented regression.
@7,
That's why you need the
call since (amongst other things) it does an include on the necessary session.inc file.