Laravel-登录时销毁现有会话 [英] Laravel - destroy existing sessions on login
问题描述
是否可以检查用户在另一台计算机上是否已有有效的会话?
Is there a way to check if a user already has a valid session on a different machine?
我想做的是,当用户登录时,销毁他们可能已经拥有的其他会话,这样,如果他们忘记了从校园或工作场所等计算机上注销,然后又在家登录,会销毁其他两个会话,使它们不再登录吗?
What I want to do is when a user logs in, destroy an other sessions which they may already have, so that if they forget to logout from a computer say on campus or at work, and then they log in at home, it will destroy those other 2 sessions so they are no longer logged in?
Facebook在某种程度上采用了这种方式.
Facebook employs this in some way.
到目前为止,我唯一的想法就是达到这种效果:
My only thoughts so far is something to this effect:
$user = User::find(1); // find the user
Auth::login($user); // log them in
Auth::logout(); // log them out hoping that it will destroy all their sessions on all machines
Auth::login($user); // log them in again so they have a valid session on this machine
我还没有机会进行测试,也不知道Auth::login($user);
是要销毁该用户的所有会话还是仅销毁当前会话.
I have not had the chance to test this, and I do not know if Auth::login($user);
will destroy all sessions for that user, or only the current one.
谢谢!
推荐答案
您可以在用户模型中保存一个session_id,以便:
You can save a session_id within a user model, so that:
-
当注销事件被触发(auth.logout)时,您将清除它.
When logout event is fired (auth.logout) you would clear it.
在触发新的日志记录事件时,您可以检查用户模型中的属性session_id是否不为空.
When new logging event is fired you can check if attribute session_id is not null within the user model.
如果不是,请通过以下方式破坏上一个会话:
If it's not - destroy previous session by:
Session :: getHandler()-> destroy($ user-> session_id);
Session::getHandler()->destroy( $user->session_id );
$ user-> session_id = Session :: getId();
$user->session_id = Session::getId();
希望有帮助!
这篇关于Laravel-登录时销毁现有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!