How to programmatically log out a user in Drupal 7?

11,575

Solution 1

You should call user_logout().

Solution 2

use this any where

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

Solution 3

Another option is using the Rules module. You can set a rule up to redirect to a URL after a user logs out and various other rules if needed.

http://drupal.org/project/rules

Share:
11,575
jini
Author by

jini

Updated on July 28, 2022

Comments

  • jini
    jini over 1 year

    I need to log a user out and redirect to a URL.

    How would I do that in Drupal 7?

    Thanks

  • Clive
    Clive about 12 years
    +1 and then drupal_goto()
  • J-P
    J-P almost 11 years
    user_logout() in D6 and D7 finish with their own drupal_goto(). I'm not convinced that your own call after that point would be processed.
  • artfulrobot
    artfulrobot over 7 years
    Not clear how this answers 2nd part of Q: how to redirect to a URL after logging out, since in D7 drupal_goto() calls user_logout() anyway.