cakephp阻止用户从多个位置同时登录 [英] cakephp prevent user login from multiple locations at the same time

查看:135
本文介绍了cakephp阻止用户从多个位置同时登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CakePHP 1.3.10网站,使用Auth登录系统。如何防止同一用户名/密码同时从不同位置登录?它应该工作方式,当一个已经登录的用户从不同的位置登录,第一个被踢出(所以最后一个可以继续使用我的网站)。

I have a CakePHP 1.3.10 website with a login system using Auth. How can I prevent a same username/password to log in at the same time from different locations? It should work in a way that when an already logged in user logs from a different location, the first one gets kicked out (so the last one can keep using my website).

我一直在做一些研究,我找不到足够好的解决方案。似乎一个简单的将一个布尔值存储为1当用户登录,但我不知道该怎么做,使这种工作方式我描述的方式。

I've been doing some research and I can't find a "good enough" solution. It seems that a simple one would be storing a boolean to 1 when a user logs in, but I don't know what to do with that to make this work the way I describe.

很容易的部分是,当用户注销时,我将该值更改为0.但是如果他只是关闭浏览器怎么办?当用户登录并且布尔值已经设置为1时,我该怎么办?

The easy part is that when the user logs out, I change that value to 0. But what if he just closes the browser? And what do I do when a user logs in and the boolean is already set to 1? How do I kick to "old" user?

任何建议都非常感谢...

Any advice would be much appreciated...

推荐答案

为了这样做,改变你的config / core.php使用数据库驱动的会话。

In order to do that, change your config/core.php to use database-driven sessions.

Configure::write('Session.save', 'database');

之后,还取消注释以下行:

After that, also uncomment the following line:

//Configure::write('Session.database', 'default');

使用默认的数据库配置是大多数情况下需要的。

Using the default DB config is what you'll want in most cases.

然后,使用Cake控制台准备用于托管会话的数据库:

Then, prepare your database for hosting the sessions by using the Cake console:

cake schema create Sessions

最后,在登录操作中,检查刚刚记录的用户是否已存在会话

Finally, in your login action, check if a session already exists for the user that just logged in and invalidate/delete their old session from the sessions table.

我以前没有使用过这个方法,但是我认为用户ID /用户名存储在会话表中。 data 字段的会话表(可能包含一个序列化数组或类似的东西)。

I haven't used this method myself before, but I figure the user id/username is stored in the data field of the sessions table (which probably holds a serialized array or something like that).

这篇关于cakephp阻止用户从多个位置同时登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆