简体   繁体   English

PHP在数据库中为每个用户管理多个会话

[英]Php Manage multiple session per user in database

I am making a login system. 我正在制作一个登录系统。 I made active_session table in database like this: 我在数据库中创建了active_session表,如下所示:

| id | user_id |session_key|others...|
| 12 |  6548   |kdjgs939493|.........|

I create a cookies with limited life time (like 1 hour or day etc). 我创建了寿命有限的Cookie(例如1小时或一天)。 When I logout manually it destroys cookies and remove session record from database as well. 当我手动注销时,它会破坏Cookie并从数据库中删除会话记录。 The problem is that if someone login and doesn't logout, the cookies will expire after it time expires but how can i remove such sessions from database. 问题是,如果有人登录但未注销,则cookie将在时间到期后过期,但是我该如何从数据库中删除此类会话。 because if these records are not deleted then there will be lot of useless sessions records in database 因为如果不删除这些记录,那么数据库中会有很多无用的会话记录

You will have to store a timestamp, either when the session was started or when it should end. 您必须在会话开始或何时结束时存储时间戳。 Then you can run scheduled tasks and delete all entries which you don't need any more. 然后,您可以运行计划的任务并删除不再需要的所有条目。

Solution 1: You can add a command to delete old entries in the same script that performs the login. 解决方案1:您可以在执行登录的同一脚本中添加命令以删除旧条目。 This way, you assure that after each login, there will be no old records. 这样,您可以确保每次登录后都不会有旧记录。

Solution 2: You can make a script that executes regularly, to check for older records in the database and delete them. 解决方案2:您可以制作一个定期执行的脚本,以检查数据库中较旧的记录并删除它们。 This way, you could use a specific mysql user for this script with right to delete records. 这样,您可以为此脚本使用特定的mysql用户,并有权删除记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM