简体   繁体   English

我如何知道 session 是否仍处于活动状态?

[英]How can i know if a session is still active or not?

I'm storing the sessions ids in a database table.我将会话 ID 存储在数据库表中。 I want to run a script that reads from the table every session id and check if this session is active or not.我想运行一个脚本,从表中读取每个 session id 并检查此 session 是否处于活动状态。

I know that session information is stored in a directory ( /tmp by default) and removed from there when the session is closed.我知道 session 信息存储在一个目录中(默认为/tmp )并在 session 关闭时从那里删除。 But if the user close his browser without disconnecting from the session the session file is still in the /tmp directory.但是,如果用户在未断开与 session 的连接的情况下关闭浏览器,则 session 文件仍位于/tmp目录中。

How can I know if a session is still active or not?我如何知道 session 是否仍处于活动状态?

I have searched the session functions and didn't find anything that could help.我已经搜索了 session 函数,但没有找到任何可以帮助的东西。

Generally speaking, the only way is to have a "last used" datetime/timestamp which you update every time the session is referenced, and discard/deactivate sessions after they haven't been used for a certain amount of time.一般来说,唯一的方法是每次引用 session 时更新“上次使用”日期时间/时间戳,并在会话未使用一段时间后丢弃/停用会话。

It's not possible to tell if a session will be referenced further as they're not kept open between requests, simply referenced when needed... and need is dictated by the ever fickle client.无法判断 session 是否会被进一步引用,因为它们不会在请求之间保持打开状态,只是在需要时引用......并且需求由变化无常的客户决定。

If your sessions are file based, as it sounds, you can use the last accessed date of the individual files, possibly save yourself a bit of trouble.如果您的会话是基于文件的,听起来,您可以使用单个文件的最后访问日期,可能会为自己省点麻烦。

If you are keeping sessions in the database.如果您在数据库中保留会话。 Add a last accessed or modified timestamp to the database table.将上次访问或修改的时间戳添加到数据库表。 I've put code in my procedures that retrieve or store session information that DELETE FROM sessions WHERE modified is more than an hour ago.我已将代码放入我的程序中,以检索或存储 session 信息,这些信息在一个多小时前从修改的会话中删除。 However long you want your session to be.无论您希望 session 有多长。 At every call you can update that timestamp.在每次通话时,您都可以更新该时间戳。 It seems that would cause a performance hit, but I've used that pattern/process quite a bit.似乎这会导致性能下降,但我已经使用了相当多的模式/过程。 I almost always use the database to store session information.我几乎总是使用数据库来存储 session 信息。 You should research session_set_save_handler() .您应该研究session_set_save_handler() I can try to gather some of my own code from previous project where I utilized this, but I'm sure that reading the manual on this and googling implementations will help you quite a bit.我可以尝试从我以前使用它的项目中收集一些我自己的代码,但我相信阅读这方面的手册和谷歌搜索实现会对你有很大帮助。

Why would you care about this already outdated sessions?你为什么要关心这个已经过时的会话? They will be cleared from temporary folder in some random time anyway.无论如何,它们将在某个随机时间从临时文件夹中清除。

You should check last access time in your database and if it's more then some predefined timeout - mark it as dead.您应该检查数据库中的上次访问时间,如果它超过一些预定义的超时 - 将其标记为已死。

But think about your performance too.但也要考虑你的表现。 It's a way better approach to store your session data in memcache or something similar to it - just store it in memory, not in DB.这是将 session 数据存储在memcache或类似的东西中的更好方法 - 只需将其存储在 memory 中,而不是存储在 DB 中。 Read more about it here: http://memcached.org/在此处阅读更多信息: http://memcached.org/

It's really easy to store your session data in memcached: http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/将 session 数据存储在 memcached 中真的很容易: http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/

There isn't such a function.没有这样的 function。 PHP itself use somewhat an hack to determine when a session is expired, there are several options into the php.ini configuration file. PHP 本身使用某种技巧来确定 session 何时过期,php.ini 配置文件中有几个选项。

In short, every time a session started there is a gc_probability / gc_divisor chanche that php will start a garbage collection of pending data.简而言之,每次 session 启动时,都会有一个gc_probability / gc_divisor chanche php 将启动待处理数据的垃圾收集。 The session accessed before gc_maxlifetime seconds are considered expired and PHP deletes them.gc_maxlifetime秒之前访问的 session 被认为已过期,PHP 将其删除。

You can relay on this behaviour tuning the envolved options or mimic this behaviour with a similar approach.您可以在此行为上进行中继,调整所涉及的选项或使用类似方法模仿此行为。

For reference:以供参考:

暂无
暂无

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

相关问题 如果用户在 javascript 应用程序在 Z226776F3561A5D33FZ0BAB 中仍然处于活动状态,我如何扩展 Joomla session? - How can i extend Joomla session if user still active on client javascript application in joomla? Wordpress如何知道分页是否有效? - Wordpress how can I know if the pagination is active? 如何查看服务器仍处于活动状态的浏览器连接? - How can I check the browser connection with server still active? 我可以知道如何在/ docs文件夹中传递zend会话吗? - Can I know how to pass the zend session in /docs folder? 如何保护PHP会话? 我仍然可以使用curl(Linux)查看源代码 - How to secure php session? I still can use curl (Linux) to see source code PHP-如果我知道session_id,如何获取会话变量 - PHP - If I know the session_id, how to get session variables 我必须用另一个xml制作一个新的xml。 我还是不知道该怎么办 - I have to make a new xml out of another xml. I still don't know how can I do it 我怎样才能知道我在点网应用程序中的php会话信息? - How can I know my php session information in dot net application? 我可以在代码加载时通过代码检查会话是否处于活动状态吗? - Can i check if a session is active throught the code instead at page load? 如何在没有活动会话的情况下停止报告PHP_SESSION_ACTIVE的session_status()? - How do I stop session_status() from reporting PHP_SESSION_ACTIVE with no active sessions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM