function user_logout

You are here

7 user.pages.inc user_logout()
4.6 user.module user_logout()
4.7 user.module user_logout()
5 user.module user_logout()
6 user.pages.inc user_logout()
8 user.module user_logout()

Menu callback; logs the current user out, and redirects to the home page.

1 call to user_logout()
7 string references to 'user_logout'
drupal-6.bare.database.php in modules/simpletest/tests/upgrade/drupal-6.bare.database.php
Bare installation of Drupal 6.17, for test purposes.
drupal-6.filled.database.php in modules/simpletest/tests/upgrade/drupal-6.filled.database.php
Filled installation of Drupal 6.17, for test purposes.
TriggerUserTokenTestCase::testUserTriggerTokenReplacement in modules/trigger/trigger.test
Tests a variety of token replacements in actions.
trigger_test_action_info in modules/trigger/tests/trigger_test.module
Implements hook_action_info().
trigger_trigger_info in modules/trigger/trigger.module
Implements hook_trigger_info().

... See full list

File

modules/user/user.pages.inc, line 170
User page callback file for the user module.

Code

function user_logout() {
  global $user;

  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();
}

Comments

How can i call this in my template.php file?
I want to automatically log out user if they use that theme.

<?php
module_load_include
('pages.inc', 'user');
user_logout();
?>

Greetings.
I have a suggestion for improvement for this function.
Soon after the session_destroy (), could be placed a drupal_set_message () to warn the user of the reason for the logout.
Would be as follows:

function user_logout($message = '', $status = 'warning') {
  global $user;

  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();

// Sends a message to the user if you have a need to warn you
  if(!empty($message)){
      drupal_set_message($message, $status);
  }
  drupal_goto();
}

Why is this suggestion?
When I'm using hook_user_login and for some reason need to kill the session user can not I put after user_logout drupal_set_message () why there is a drupal_goto () and also no point in putting before why there is a session.destroy () which kills with the entire session.

The only way I saw to solve this is after putting session.destroy ().

[]'s

I have an imporovment on this function, why not have a paramater with default value to indicate where to go after the logout.

<?php
function user_logout($destination = '') {
  global
$user;

 

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($destination);
}
?>

i agree with artofeclipse, but the destination should be provided by the hook, so it should be possible to interact and change the destination by changing the destination and the user_logout function redirects to this destination

Couldn't you just do...

mymodule_user_logout($account) {
  $_GET['destination'] = 'path/to/redirect/to/after/logout';
}