检查 PHP SessionID 是否正在使用?
在我的一个项目中,我使用 sessionId 在数据库中存储临时数据,如果其 sessionId 未使用,我必须删除它们,是否有一种简单的方法来检查会话 id 是否正在使用?
at one of my project, i store a temporary data in database with a sessionId, and i've to delete them if its sessionId is not in use, is there a simple way to check if session id is in use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用默认会话处理程序,则检查会话文件是否存在并且自 time()-session_cache_expire() 以来是否已被修改。
然而,更好的解决方案是使用数据库绑定的会话处理程序。当尝试从会话外部查看会话数据时,这要灵活得多,您只需执行简单的连接即可查找匹配的数据,但解决问题的正确方法是在会话处理程序中实现会话管理代码(即删除会话时也删除数据)!
If you are using the default session handler then checking if the session file is present and has been modified since time()-session_cache_expire().
However a much better solution would be to use a database-bound session handler. This is much more flexible when trying to look at session data from outside the session, you could just do a simple join to find the matching data, but the right way to solve the problem is to implement the session management code within the session handler (i.e. delete the data when the session is deleted)!
存储上次访问的时间戳,并删除那些时间早于当前时间 X 分钟的行。
Store a last accessed timestamp, and delete those rows whose time is X minutes earlier than the current time.
您可以使用 session_name ([ string $name ] ) 命名会话,
例如:
session_name('myname')
,然后只需使用 session_id ([ string $ id ] ),
示例:
You can name your session with session_name ([ string $name ] ),
example:
session_name('myname')
,and then just check if there is a session id for that name with session_id ([ string $id ] ),
example: