logout programmatically with joomla

user1075778 picture user1075778 · Dec 1, 2011 · Viewed 8.3k times · Source

I need to customize a method in a component. I need to make different action (deleting a user and other info) and then logout the user programmatically (not with a button link), how can I achieve this?

I've tried to do this at the end of the method:

$return   = JRoute::_('index.php?option=com_users&task=user.logout', true);
$this->setRedirect($return,$msg);

But this gives a invalid token message.

Thanks

Answer

alghimo picture alghimo · Dec 1, 2011

What Joomla version are you working on? If it's Joomla 1.7, you can do this in your code:

$app = JFactory::getApplication();
$app->logout( $user_id );

Where $user_id is the ID of the user you want to log out. If you leave it empty or do not set it, it will logout the user that's performing the request. For Joomla 1.5, you can do it the same way, but in Joomla 1.5 there's a global variable named $mainframe that holds an instance of the app, so you can do:

global $mainframe;
$mainframe->logout( $user_id );

I hope it helped!