A drupal_set_message() issued during the logout process is lost and never displayed.

What I did: Created an system_message_action() and associated it with the user_logout trigger.

What I expected: A message displayed to the user after logout.

What happened instead: No message was displayed.

I imagine this is because the session is destroyed, and the messages are queued in the session. This is not the expected behavior, though.

Solution for D7 when writing custom code:

      module_load_include('pages.inc', 'user');
      user_logout_current_user();
      drupal_set_message('My message will be shown to the user after they are logged out.');
      drupal_goto();

**Next Steps**
1. Confirm if there's a similar approach for D8/D9/D10.
2. Confirm if higher-level APIs (like the action/trigger API) exhibit this problem. If so, can they be modified using the above technique?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rfay’s picture

The same problem occurs, for the same reason, I'm sure, at user account creation/login.

1. Create a system message action and associate it with user_insert trigger.
2. Create a user account.
3. No message is displayed.

The trigger fires, the drupal_set_message() is executed. But nothing is displayed.

rfay’s picture

Title: Messages lost during logout » Messages lost during logout and during user creation/login
andypost’s picture

Session should be regenerated not destroyed. I'm trying to look latter

Damien Tournoud’s picture

Status: Active » Closed (duplicate)

I believe this is delt with in the (still incomplete #813492: HTTPS sessions use an invalid merge query).

rfay’s picture

If we can get #721086: Create tests for system actions, clean up token replacement, clean up triggers done and then if #813492: HTTPS sessions use an invalid merge query can make progress, we need to add to it the attached patch, which will properly test.

rfay’s picture

#813492: HTTPS sessions use an invalid merge query has been committed, so it's time to see if this was resolved.

Damien Tournoud’s picture

Priority: Normal » Critical
Status: Closed (duplicate) » Active

Temporarily un-duplicating and bumping, to keep this on the radar.

Damien Tournoud’s picture

Checked manually (the test modified by #5 seems to be gone):

- messages triggered on logout are still lost in oblivion
- messages triggered on login and user insert work

Damien Tournoud’s picture

Title: Messages lost during logout and during user creation/login » Messages lost during logout
Priority: Critical » Normal

Downgrading. This only affects logout, and looks by-design to me:

  watchdog('user', 'Session closed for %name.', array('%name' => $user->name));

  module_invoke_all('user_logout', $user);

  // Destroy the current session, and reset $user to the anonymous user.
  session_destroy();

  drupal_goto();

We could consider being slightly less heavy-handed here (for example by defining a hook to prevent part of the session from being destroyed). But at least this works as designed.

rfay’s picture

The test modified by #5 is in #721086: Create tests for system actions, clean up token replacement, clean up triggers and is here as a placeholder in case it can go back into that one later (it was commented out in that one).

Eronarn’s picture

Just in case anyone Googling this runs across this issue - drupal_set_message stores the message in the database, tied to session. user_logout destroys the session and does a drupal_goto. Users will get a new session as soon as they load the drupal_goto'd page, but there's no way to link that to the message, which was associated with their old session. To get the drupal_set_message functionality without something like including a querystring and checking for it on the destination page, we need to give them their new session *before* the goto. Here's how I did that:

1) Call session_get_cookie_params to get the current session's attributes
2) Call session_destroy to end the current session
3) Call session_set_cookie_params based on the values returned by 1)
4) Call session_start to start a new session
5) Log the user out with $user = drupal_anonymous_user(session_id()); instead of just $user = drupal_anonymous_user();
6) drupal_set_message followed by drupal_goto will now work.

I don't know if this is the safest/sanest approach, regarding e.g. attacks on session ID, but it at least works for my simple use case. Suggestions for refinement would be appreciated.

(This was in D6, by the way, but the logout code is similar.)

bleen’s picture

in #11.5 did you mean "Log the user IN...", not "Log the user out..." If not, huh?

Eronarn’s picture

Yes, good catch, thanks.

DamienMcKenna’s picture

This affects Drupal 6 too. Would like to see it resolved for both D6 and D7 as doing a drupal_set_message on hook_user('logout') is beneficial for the user experience.

DamienMcKenna’s picture

@eronarn: I tried out your idea with the following code:

  // 1) Call session_get_cookie_params to get the current session's attributes
  $p = session_get_cookie_params();
  // 2) Call session_destroy to end the current session
  session_destroy();
  // 3) Call session_set_cookie_params based on the values returned by 1)
  session_set_cookie_params($p['lifetime'], $p['path'], $p['domain'], $p['secure'], $p['httponly']);
  // 4) Call session_start to start a new session
  session_start();
  // 5) Log the user out with $user = drupal_anonymous_user(session_id());
  // instead of just $user = drupal_anonymous_user();
  $user = drupal_anonymous_user(session_id());
  // 6) drupal_set_message followed by drupal_goto will now work.
  drupal_goto();

Unfortunately it gives the following error:
Fatal error: session_start() [<a href='function.session-start'>function.session-start</a>]: Failed to initialize storage module: user (path: /Applications/MAMP/tmp/php) in /modules/user/user.pages.inc on line 157
Going to dive into it some more.

DamienMcKenna’s picture

I've resolved the error by changing the session_start part to:

  session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy_sid', 'sess_gc');
  session_start();

Now to retain the messages.

DamienMcKenna’s picture

Status: Needs review » Active
FileSize
1.23 KB

Here's a patch against Drupal 6.

Patch sponsored by the Siebel Scholars Foundation.

DamienMcKenna’s picture

Status: Active » Needs review
dmitrig01’s picture

Status: Active » Needs work

no one-letter variable names. as for the rest, i have no idea

DamienMcKenna’s picture

Changed $p to $params.

DamienMcKenna’s picture

Status: Needs work » Needs review
FileSize
1.1 KB
1.26 KB

Patches for both D6 and D7.

Status: Needs review » Needs work

The last submitted patch, drupal-logout_session-n754560-20.patch, failed testing.

akoepke’s picture

Status: Needs work » Needs review
Akaoni’s picture

Title: Messages lost during logout » Session contents lost during logout
Status: Needs review » Needs work

This is still a problem and the scope is larger than originally outlined. Any other modules that set $_SESSION variables which aren't tied to the login lose their data on logout.

The D7 patch is outdated and needs reworking. Not sure about the D6 patch.

andypost’s picture

Version: 7.x-dev » 8.x-dev

Moving to 8.x

+++ modules/user/user.pages.inc	17 Jan 2011 18:27:54 -0000
@@ -170,9 +170,22 @@ function user_logout() {
+  // Obtain the current session's attributes.

This part cares only about session params but other content stored is going to be lost

Powered by Dreditor.

rfay’s picture

Issue tags: +Needs backport to D7

Setting to needs backport

pjcard’s picture

subscribe (sorry if this is the wrong way to do it, but I can't see a monitor/subscribe button anywhere).

rfay’s picture

Status: Needs work » Needs review
FileSize
949 bytes

Here is #21 rerolled for D8.

Damien Tournoud’s picture

Status: Needs review » Needs work

Related: #758730: Fatal error on logout.

The route forward seems to be what I asked for in #9: we implement drupal_session_destroy() as described in #758730: Fatal error on logout and we add a hook in there that allow contrib modules to rescue part of $_SESSION.

Akaoni’s picture

Issue tags: +session

Adding session tag.

moshe weitzman’s picture

Priority: Normal » Minor

IMO, this is a small problem, not worthy a complex solution. Code that wants to set a message after logout should try to run after the destroy.

Akaoni’s picture

Here's a simple solution: don't destroy the session on logout.

Session != login
Drupal seems to make this assumption, however.

lmeurs’s picture

Ran into the same problem with Drupal 7.12: after logging out all shopping cart related data (custom module) is gone...

nedjo’s picture

@moshe weitzman:

Code that wants to set a message after logout should try to run after the destroy.

Could you sketch in how this might work? Sample use case: using Rules, log out a user and present them a message about having been logged out. Currently, this would be done with two actions: (a) set a message and (b) redirect (to user/logout). But if the message is set before redirecting, it is never presented to the user as the session ends. If it is set after redirecting, it is never executed because the page execution (and session) have ended. How could a rule action execute after the destroy?

nedjo’s picture

+++ b/modules/user/user.pages.inc
@@ -171,9 +171,22 @@ function user_logout() {
+  session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy_sid', 'sess_gc');

For D7+ the session calls are renamed, see http://drupal.org/node/224333#session_rename.

yingtho’s picture

Version: 8.x-dev » 7.x-dev
FileSize
1.11 KB

I have added so it retains all the old session data. This patch is for drupal 7.

yingtho’s picture

Status: Needs work » Needs review

Forgot to set the correct status.

Status: Needs review » Needs work

The last submitted patch, drupal.logout-session_754560_36-d7.patch, failed testing.

yingtho’s picture

One thing to think about is security as it will retain all the previous session data. So if a module that don't want data to be retained need to implement hook_user_logout and remove the session data there.

jacobfriis’s picture

@yingtho #39
Agreed! Regenerating the whole session array is hazardous, not what you would expect, and acutely contrary to the concept of being logged in (versus not).
Only the 'messages' bucket - $_SESSION['messages'] - should be regenerated (and that only if non-empty ;-)

Relaying the responsibility of destroying session data to modules is not correct - and would btw never be carried into effect in the real world.

jacobfriis’s picture

Set Drupal message that survives session destruction (like user logout):
Module State - state_set_message(); documentation.

steinmb’s picture

Version: 7.x-dev » 8.0.x-dev
Issue summary: View changes

Do we still need to address this in D8, or have session handling changed to such a degree that this issue only apply to D7?

andypost’s picture

jacobfriis’s picture

Reproduction in D7:

drupal_set_message('You\'ll never see this message...');
module_load_include('pages.inc', 'user');
user_logout();

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jcisio’s picture

Version: 8.6.x-dev » 9.0.x-dev

Thanks @DamienMcKenna for the 9 year old D6 patch.

hash6’s picture

Assigned: Unassigned » hash6
xjm’s picture

Version: 9.0.x-dev » 8.8.x-dev

We would still want to fix this bug in D8 as well. Thanks!

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

catch’s picture

Title: Session contents lost during logout » Messages are lost if set just prior to logout
Issue tags: +Bug Smash Initiative, +Needs issue summary update

Retitling this one.

dalin’s picture

At least on D7, core has been refactored a bit since the last patch from 8yrs ago. The solution now (probably similar on D8) is something like this:

      module_load_include('pages.inc', 'user');
      user_logout_current_user();
      drupal_set_message('My message that the user will see after they log out.');
      drupal_goto();
apaderno’s picture

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8.9.x is now open only to security issues.

dalin’s picture

Issue summary: View changes
dalin’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Status: Needs work » Closed (works as designed)

I think this is doable in D9+ as follows:

Implement hook_user_logout which gets called before logout, set some sort of flag that a user is being logged out.

Add an event listener for the \Drupal\Core\Session\AccountEvents::SET_USER event, if the flag from above is set and the new set user is anonymous, add the message, if not, unset the flag.

Bit janky to have a hook for before and event for after, but the APIs are there.

DamienMcKenna’s picture

Should this not be fixed in core?

larowlan’s picture

Are you meaning that core copies the messages from one session to the next?

DamienMcKenna’s picture

Yes.

larowlan’s picture

Status: Closed (works as designed) » Active

Ok, then let's keep it open

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.