Is there a hook or something similar that triggers when a PHP session ends?

When a user signs in, I'm writing a few things to the cache, and I need to delete them from the cache when the user exists the site. If the user manually logs out, that's no problem, but I still need to clear that data when users close the browser.

3 Answers 3


Drupal doesn't have hooks that are invoked when a session ends, probably also because the code would not be able to understand if the session cookie is not present because it expired (which means the session ended) or because the browser has not been used to log in on a Drupal site.

Drupal invokes a hook when the user is logged out, either because the user clicked on the Log out link, or because the user was forced to log out, which is what happens when the site is in maintenance mode and the account isn't exempted from being automatically logged-out.
Drupal 7 and Drupal 8 invoke hook_user_logout() when the user is logged-out, and the code invoking the hook also destroys the user session.

Since you are talking of cache to clear, if you are using Drupal 8, you can also investigate if there are cache tags or contexts that tell Drupal to invalidate the data when the session is destroyed.
Cache contexts lists session.exists as context that can be associated to cache data. I didn't test it, but from the context name, I take it invalidates the cache data when the session doesn't exist, which is what you are trying to achieve. (See also Cache API for more information on cache tags and cache contexts in Drupal 8.)

As side note, a Drupal session doesn't normally end when users close their browser. It would happen when using an incognito window with Google Chrome, or the equivalent of other browsers, but when you are not using that, the session doesn't end when the browser is closed, or when the browser tab is closed. (If that were to happen, it would mean logging off the users.)


There is no Drupal hook for session management. The closest thing [to inserting your own custom code in session management] is overriding core session management functions.

While you could simply copy/paste/override (existing session management code) to hook into execution, you should consider an alternative seeing how session cache cleanup can be performed without hooking into session management (e.g. cronjob, etc).


There is no possible way you can know when a user closes the browser. But as a general rule if something is written to cache, it should be disposable, and can be regenerated on demand if necessary. Cache is not meant for data that has any value in itself.

Assuming that is the case here, what you need to do is to ensure your cached items expire after a while. Use a timestamp value for the expiry option of cache_set() for Drupal 7 or CacheBackendInterface::set() for Drupal 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.