14

I know I can display the login/logout form with the following link:

http://example.com/index.php?option=com_users&view=login

But once logged in, this link will redirect the user to a page with a logout button that has to be clicked.

Is it possible to create a link that will log out the current user directly?

24

The following link is for direct logout:

http://example.com/index.php?option=com_users&task=user.logout

But a user token is required in order to avoid an error message Invalid Token, and some PHP magic is required in order to get the token. The following code will display a Login link to guests, and a direct Logout Username link to logged in users:

$user = JFactory::getUser();
if ($user->guest)
{
    echo '<a href="index.php?option=com_users&view=login">Login</a>';
}
else
{
    $userToken = JSession::getFormToken();
    echo '<a href="index.php?option=com_users&task=user.logout&' . $userToken . '=1">Logout '  . $user->username . '</a>.';
}

The code can be included in any extension, or using a plugin/module that allows inserting PHP code directly (e.g. DirectPHP, Custom HTML Advanced, Sourcerer or similar).

  • 1
    Haha, too obvious...? Sorry about that. To be fair, though, I find the code snippet quite usefull, and use it all the time on client websites to have a small, non-intrusive login link in the footer. I just wasn't sure about what to ask in order to post the code. – johanpw Dec 17 '14 at 11:52
  • +1 Extremely useful! How can you make a redirect to a specific page after it? is there some &additional_parameter that can be used to allow a redirect after logout? – Gruber Jul 29 '15 at 1:33
  • Hey there thanks for this! Can you provide some documentation where this is explained a bit more in detail? Thank you! – Gruber Jun 12 at 4:48
7

There is already a plugin which works great for logout button.

You should check this extension logout button

6

As a final answer, Joomla has a builtin "Logout" menu item, added to core I think around 3.4* maybe 3.5. So now you don't need any additional plugins.

The logout menu item also allows for a redirect on logout.

Also if you set your Login menu item to 'Guest' access and your Logout menu item to 'Registered' access, the menu change depending on your status.

Detailed SE answer here: Change Login to Logout

  • This is probably the best approach for websites using an up to date version of Joomla since it only uses Joomla core functionalities. If you really need to programmatically create a logout link, however, the "accepted answer" is a good solution. – feculededentier Oct 4 '16 at 18:29
4

As a third option, there is a Quick Logout component which adds a direct logout menu item in Joomla and will logout the users without the confirmation.

It also provides the option to set redirection to a desired page after logout.

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.