How to programmatically log out a user in Drupal 7?
Asked Answered
S

3

8

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

How would I do that in Drupal 7?

Thanks

Smalt answered 30/3, 2012 at 18:45 Comment(0)
M
18

You should call user_logout().

Mungo answered 30/3, 2012 at 18:46 Comment(3)
+1 and then drupal_goto()Duckpin
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.Qp
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.Adapt
B
7

use this any where

 <?php
module_load_include('pages.inc', 'user');
user_logout();
?>
Brazil answered 22/7, 2014 at 7:30 Comment(0)
I
2

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

Impasto answered 1/4, 2012 at 3:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.