简体   繁体   English

以编程方式注销joomla

[英]logout programmatically with joomla

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 谢谢

What Joomla version are you working on? 你正在做什么Joomla版本? If it's Joomla 1.7, you can do this in your code: 如果它是Joomla 1.7,您可以在代码中执行此操作:

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

Where $user_id is the ID of the user you want to log out. 其中$ user_id是您要注销的用户的ID。 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: 对于Joomla 1.5,您可以采用相同的方式,但在Joomla 1.5中有一个名为$ mainframe的全局变量,它包含应用程序的实例,因此您可以执行以下操作:

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

I hope it helped! 我希望它有所帮助!

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

Joomla 4.0 they use namespace concept. Joomla 4.0他们使用命名空间概念。

For Joomla 3.x use 适用于Joomla 3.x使用

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

For Joomla 4.x use 对于Joomla 4.x使用

\Joomla\CMS\Factory::getApplication();
$app->logout($user_id);

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

If it's Joomla 2.5 You can use Joomla's default login module (which has a logout button as well). 如果它是Joomla 2.5你可以使用Joomla的默认登录模块(它也有一个注销按钮)。 Go to Extensions >> Module Manager >> New >> Login >> configure appropriately and save. 转到扩展>>模块管理器>>新建>>登录>>正确配置并保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM