如何在Laravel中使特定会话无效(与用户一起使用“记住我"功能) [英] How to invalidate particular session in Laravel (with user using remember me feature)
问题描述
使用 Laravel 5.1& 文件"会话驱动程序,
我试图通过在数据库中保留其session_id
的记录来为用户提供跟踪他们的会话并使它们无效的便利.我的意思是,对于数据库,我维护一个名为user_sessions
的表,该表将user_id
与它们的session_id
(由Session::getId()
获取)相关联.
I'm trying to provide facility to user to track their sessions and invalidate them anytime they wish by keeping a record of their session_id
within the database. With Database, I mean, I maintain a table called user_sessions
which associates user_id
with their session_id
(obtained by Session::getId()
).
因此,为了使Session无效,我尝试了以下代码
So, to invalidate Session, I tried the following code,
$sessionId = Session::getId();
Session::setId($sessionId);
Session::invalidate();
,并且在用户不使用记住我"功能的情况下,它工作得很好.
对于用户使用记住我"功能的情况,上面的代码不起作用,因此,我还尝试将remember_token
字段设置为null
在此答案中指定的位置,但与此同时,该用户的所有会话(包括当前会话)都将被销毁.
For the case where user uses Remember Me feature, this above code does not work, So, I additionally, tried setting remember_token
field to null
as specified here in this answer, but with this, all sessions of the user get destroyed including the current one.
推荐答案
您可以简单地使用
use Illuminate\Support\Facades\Session;
Session::forget('YOUR_SESSION_ID');
如果要获取当前会话ID:
If you want to get the current session Id:
Session::driver()->getId();
我希望对您有帮助
这篇关于如何在Laravel中使特定会话无效(与用户一起使用“记住我"功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!