2

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.

  • As 4k4 mentioned, just call user_logout() which will simply destroy the session. – user21641 Sep 15 '17 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 Sep 15 '17 at 19:55
5

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.

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.