8

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

César
  • 9,939
  • 6
  • 53
  • 74
user1075778
  • 81
  • 1
  • 2
  • 1
    According to https://api.joomla.org/cms-3/classes/JApplication.html#method_logout the logout function has been deprecated as of 4.0. Is it still usable? If not, does anyone have an alternate solution? – Sameer Puri May 02 '15 at 14:54

6 Answers6

19

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!

alghimo
  • 2,899
  • 18
  • 11
4
$app = JFactory::getApplication();
$app->logout();
András
  • 3,395
  • 1
  • 21
  • 27
4

Joomla 4.0 they use namespace concept.

For Joomla 3.x use

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

For Joomla 4.x use

\Joomla\CMS\Factory::getApplication();
$app->logout($user_id);
Alagesan
  • 349
  • 1
  • 11
0

One alternative which worked for me is to use the below method. You might need to redirect after this function call.

$cmsApp = new JApplicationCms;
$cmsApp->logout();

Or you can also redirect to logout action of user controller but this will only work for currently logged in user.

// For site
$logoutUrl   = 'index.php?option=com_users&task=user.logout&'. JSession::getFormToken() .'=1';
// For admin
$logoutUrl   = 'index.php?option=com_login&task=logout&'. JSession::getFormToken() .'=1';
$app    = JFactory::getApplication();
$app->redirect(JRoute::_($logoutUrl, false));
Irfan
  • 7,029
  • 3
  • 42
  • 57
-1
Solution 1 :
$app = JFactory::getApplication();              
$user = JFactory::getUser();
$user_id = $user->get('id');            
$app->logout($user_id, array());

Solution 2:
$app = JFactory::getApplication();   
$userToken = JSession::getFormToken();
$msg = 'Logout';
$return   = JURI::ROOT().'index.php?option=com_users&task=user.logout&'.$userToken . '=1';
$app->redirect($return,$msg);
Josef Amalraj
  • 453
  • 3
  • 11
  • Solution 1 is still using the JApplication->logout() function which is marked as deprecated. Solution 2 is merely a hack and unusable if I want to logout someone else. – Deckard Mar 28 '18 at 09:07
  • Sloution2 Just trigger any function its worked.am using my projects – Josef Amalraj Mar 28 '18 at 11:43
-2

If it's Joomla 2.5 You can use Joomla's default login module (which has a logout button as well). Go to Extensions >> Module Manager >> New >> Login >> configure appropriately and save.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
  • Of course "not by button click", config page that I refer to is providing a way how to config this event (as user wants log-out on deleting a user). – Yevgeniy Afanasyev May 19 '16 at 05:14