Laravel:一次只允许每个用户一个会话 [英] Laravel: Only allowing one session per user at a time
问题描述
在Laravel 4中,每个用户一次只能允许一个会话吗?
Is it possible in Laravel 4 to only allow one session per user at a time?
例如,如果用户登录并且已经有其他会话,则这些其他会话将被取消,最好是通过一种方式来提醒他们为什么?
Eg, so if User logs in and already has other Sessions, those other Sessions are cancelled, preferably with a way to alert them why?
如果有帮助,我正在Amazon ElastiCache上使用Redis会话驱动程序.
I'm using the Redis session driver on Amazon ElastiCache if that helps as well.
推荐答案
是的,我为我的项目做过类似的事情.
Yeah, I did similar for my project.
因此,在这种情况下,您需要向用户模型添加另一个属性:last_sessid.
So, you need add to your user model another attribute in this case: last_sessid.
public function swapping($user) {
$new_sessid = \Session::getId(); //get new session_id after user sign in
$last_session = \Session::getHandler()->read($user->last_sessid); // retrive last session
if ($last_session) {
if (\Session::getHandler()->destroy($user->last_sessid)) {
// session was destroyed
}
}
$user->last_sessid = $new_sessid;
$user->save();
}
现在,如果用户有一个活动会话,并且登录了另一个浏览器,则第一个会话将被删除.
Now, if the user has an active session, and signs in another browser, the first session will be removed.
P.S.对不起,我的英语不好:)
P.S. Sorry for my bad english :)
这篇关于Laravel:一次只允许每个用户一个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!