php - 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.
解决方案
如果您的最终目标是让任何给定帐户一次只能登录到一台机器,请在登录时生成一个唯一 ID 并将该 ID 写入该用户的数据库。将该 ID 设置为用户的 cookie。当您收到来自该用户的流量时,仅当他们的 cookie 与数据库中的值匹配时才认为他们已登录。
当用户登录到新设备时,会生成一个新的唯一 ID,并将其作为 cookie 发送到该新设备。新设备的流量有一个与数据库匹配的 cookie,因此被视为已登录。当旧设备访问您的应用程序时,登录 cookie 不再与数据库中的值匹配,因此该用户被视为已注销。
当旧设备再次登录时,数据库中会生成一个新的唯一 ID,并作为 cookie 发送到该设备。他们现在已登录,因为他们的 cookie 匹配。第二台设备的 cookie 不再与数据库匹配,将被注销。
推荐阅读
- python - pygame音频错误无法识别的音频格式
- javascript - 在子组件中定义方法以使用 ref 挂钩 - 反应原生功能组件
- c# - 使用 AJAX vs SQL 数据源读取数据表
- android - Android+crypto:导出公钥,使用openssl加密,然后回读
- python - 使用 numpy 的相似度度量
- python - Kivy MDToolBar right_action_items:无法更改屏幕
- aframe - A 帧 gltf 纹理似乎倒置
- r - 变异的R问题
- swift - 将图像保存到 .documentsDirectory,并作为字符串存储在本地领域中的路径
- c++ - 二分查找的终点