首页 > 解决方案 > Best way to prevent simultaneous login

问题描述

I know this is a recursive question, but, I haven't found a new solution, or a solution based on the new frontend frameworks or technologies.

I've a Vue + PHP application that users can olny log once per time. My current solution to block concurrent access is making a call to a PHP page with Ajax from 5 to 5 minutes storing the time. I store a flag in DB too, whether it has been registered or not. So, when the user try to log in, I check if the time is greater than 6 minutes or the flag is set to 0.

I think this is not the best way to do this. When the application has too many users it can cause too much load on the server.

There is a way to do like Netflix? An warn when triyng to connect and was logged in another machine.

标签: phpvue.js

解决方案


如果您的最终目标是让任何给定帐户一次只能登录到一台机器,请在登录时生成一个唯一 ID 并将该 ID 写入该用户的数据库。将该 ID 设置为用户的 cookie。当您收到来自该用户的流量时,仅当他们的 cookie 与数据库中的值匹配时才认为他们已登录。

当用户登录到新设备时,会生成一个新的唯一 ID,并将其作为 cookie 发送到该新设备。新设备的流量有一个与数据库匹配的 cookie,因此被视为已登录。当旧设备访问您的应用程序时,登录 cookie 不再与数据库中的值匹配,因此该用户被视为已注销。

当旧设备再次登录时,数据库中会生成一个新的唯一 ID,并作为 cookie 发送到该设备。他们现在已登录,因为他们的 cookie 匹配。第二台设备的 cookie 不再与数据库匹配,将被注销。


推荐阅读