Hello StackOverflow社区:
这是一个棘手的情况。假设有20个用户已登录到我的webapp,而我,管理员(来自不同的计算机和浏览器)禁止其中的3个,那么我如何将这3个已登录的用户踢出去我刚刚禁止?这似乎是删除会话/ cookie的问题,但我如何知道要删除/无效的会话以及如何访问它们?
您知道,这个项目是在CakePHP中,我使用Memcache作为存储会话的引擎。我已经尝试了cakephp频道中讨论的选项,如下所示:
当用户登录时,他的session_id(例如sd19eIVasdokja021dnasd)与他的用户ID(例如db:323中的那个)一起存储在memcached中。这样,用户的db记录与服务器中的session_id相关联。一些代码:
Cache::write('user_session_id_' . $this->Auth->user('id'), $this->Session->id());
在模型将用户的禁止列设置为1之后,我查找是否存在具有用户ID的Memcache密钥,从中检索会话ID。然后我删除了Memcache密钥:
if ($this->save(array('User' => array('id' => $userId, 'banned' => 1)), false)) {
$userSessionId = Cache::read('user_session_id_' . $userId);
if ($userSessionId !== false) {
Cache::delete($userSessionId);
Cache::delete('user_session_id_' . $userId);
}
}
它不起作用,用户仍然登录。我非常肯定我必须销毁/使cookie无效,在这种情况下,可能无法改变其他用户的cookie,对吗?
关注和解决方案:
我能够通过以下方式解决这个问题...... Remotely destroy a session in php (user logs in somewhere else)?
...还有这个(User
模型中的一些伪代码实际上是从我上面的第二点开始演变而来的)(请注意,由于第六行,我也需要上面列出的第一点)下文):
foreach ($bannedUsers as $userId) {
if ($this->save(array('User' => array(
'id' => $userId,
'banned' => 1,
'ban_date' => date('Y-m-d H:i:s'))), false)) {
$userSessionId = Cache::read('user_session_id_' . $userId);
if ($userSessionId !== false) {
$Session->id($userSessionId);
$Session->write('Auth', '');
Cache::delete('user_session_id_' . $userId);
}
}
}
答案 0 :(得分:1)
您的应用似乎做错了。 SESSION / COOKIE系统的想法是,在使用浏览器发送的COOKIE向服务器发出的每个请求开始时,存储的SESSION id匹配。如果它们不匹配,应用程序只会退出并显示登录页面。因此,如果您能够正确删除SESSION ID,除非您没有在每页的第一行检查SESSION / COOKIE匹配,否则无法注销特定用户