I need to create a button to log out in Joomla, I mean, the user usually enter the session but then must press the button I created to close this session, I know how to check if any user has entered the session and I know how to display the button, what I don't know is how to make that button close the actual user session (log out).
This is the base code I have:
<?php $user =& JFactory::getUser(); ?>
<?php if ( ($user->id)==0 ) : ?>
//***code for not opened session***
<?php else : ?>
<form id="form1" name="form1" method="post" action="">
<input type="button" name="button" id="button" value="Close Session" />
</form>
<?php endif ?>
How to make that button to close the joomla 2.5 session, I checked the API page but i didn't find it.
To create a log out button, you could create a link with the class "button".
In Joomla > 1.7 (also 2.5.x) you need the JUtility::getToken() part to make the login successful: Use the optional "return" part for redirecting the user back to the page where the user was when clicking the button
<a class="button" href="<?php echo JRoute::_('index.php?option=com_users&task=user.logout&'. JUtility::getToken().'=1'); ?>">
Logout
</a>
If you want to redirect back to the page where the user was when he/she clicked the logout button, add a base64 encoded return parameter:
<a class="button" href="<?php echo JRoute::_('index.php?option=com_users&task=user.logout&'. JUtility::getToken().'=1&return='.base64_encode(JURI::current())); ?>">
Logout
</a>
the correct link is actually
JRoute::_('index.php?option=com_users&task=user.logout&'. JUtility::getToken() .'=1');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With