I am trying to display message or run some other action when user logout and redirect them to home page. I am trying following code, but all I get is just set_message and it never logoff me. why?

 * Implements hook_user_logout().
function ucenter_user_logout($account) {
  drupal_set_message(t('Thanks for visiting, come again!'));

Calling drupal_goto(), you are avoiding the session is destroyed. The function that invokes hook_user_logout() (user_logout()) contains the following code.

  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.


drupal_goto() calls drupal_exit(), which means that with your Drupal's execution is interrupted before session_destroy() is called.

What you can do is changing the code to something similar to the following one.

function ucenter_user_logout($account) {
  user_cookie_save(array('ucenter.logoff' => '1'));

function ucenter_init() {
  // Check the value of the cookie Drupal_visitor_ucenter_logoff
  // and print the message if the user precedently logged off.
  • Hi kiamlaluno, I just want to display a message, when user logout, How to do that? As I know, hook_user_logout can not use drupal_set_message – Brian Lau Sep 26 '12 at 2:29
  • 1
    You cannot use drupal_set_message() because the messages are saved in the session, which is then destroyed. I have updated my answer. – kiamlaluno Sep 26 '12 at 2:42
  • Thanks, It works fine now. but its not working when I add a condition: if($_COOKIE['Drupal.visitor.ucenter.logoff'] == 1){drupal_set_message('hello')}, whats wrong? – Brian Lau Sep 26 '12 at 3:22
  • I know why, need to replace '.' to '_', if($_COOKIE['Drupal_visitor_ucenter_logoff'] == 1),thank you very much – Brian Lau Sep 26 '12 at 4:01
  • The name of the cookie you read with $_COOKIE is Drupal_visitor_ucenter_logoff; the dots are replaced with underscores, when the cookie is saved in the browser. – kiamlaluno Sep 26 '12 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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