4

I need to logout the current user in my code. For now I use : user_logout(); but this function seems to have a redirect at the end.

How can I logout a user without any redirect ?

It need to be for a single case, not a hook to every logout.

Thanks.

2
  • As 4k4 mentioned, just call user_logout() which will simply destroy the session.
    – user21641
    Commented Sep 15, 2017 at 9:57
  • "without a redirect". And what should happen then? The thing is that even though you call user_logout(), you've still bootstrapped drupal with the current user logged in, so unexpected things might happen if you then display that page after destroying the sesion. You could for example redirect back to the current page, going to user/logout?destination=foo will for example go to /foo after logout
    – Berdir
    Commented Sep 15, 2017 at 19:55

2 Answers 2

6

The logout controller (UserController::logout()) does this in a separate step.

  public function logout() {
    user_logout();
    return $this->redirect('<front>');
  }

You can alter the controller class for the user.logout route to replace this with custom code.

What do you want to do instead? Building an anonymous response a redirect seems to be the most straightforward solution.

0

Just throwing this in for Drupal 7, since Google sent me here (I am aware the question is for Drupal 8, but this answer should be valid for 8 too?)

user_logout_current_user(); appears to perform this without the redirect.

https://api.drupal.org/api/drupal/modules!user!user.pages.inc/function/user_logout/7.x https://api.drupal.org/api/drupal/modules%21user%21user.pages.inc/function/user_logout_current_user/7.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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