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).