apache PHP会话超时回调?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1287064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP session timeout callback?
提问by Atlas
I'm running a PHP + APACHE + CENTOS Linux combination.
我正在运行 PHP + APACHE + CENTOS Linux 组合。
I have implemented a login & logout on the website.
我已经在网站上实现了登录和注销。
My question is, how can I know when the php session has timed-out (User has closed his browser without logging-out)?
我的问题是,我怎么知道 php 会话何时超时(用户在没有注销的情况下关闭了浏览器)?
The reason is, I want perform some cleanups and/or database updates (calling another PHP) when the user has done any of the following: (1) LOGGED-OUT or (2) TIMED-OUT
原因是,当用户执行以下任何操作时,我想执行一些清理和/或数据库更新(调用另一个 PHP):(1) LOGGED-OUT 或 (2) TIMED-OUT
My guess is that I would have to make use of Apache/Linux, right?
我的猜测是我必须使用 Apache/Linux,对吗?
回答by gnarf
Instead of detecting when the php session times out, you could create a script that will run at some semi regular interval (every 5 minutes say, using a crontab job most likely) that will log out/perform the clean up for anyone who hasn't been active in the last hour (or whatever your timeout value is) automatically. Just add a 'lastseen' to your user table.
除了检测 php 会话何时超时,您还可以创建一个脚本,该脚本将以某个半定期间隔运行(例如每 5 分钟一次,最有可能使用 crontab 作业),该脚本将为没有的任何人注销/执行清理t 自动在过去一小时(或任何您的超时值)处于活动状态。只需在您的用户表中添加一个“lastseen”。
回答by Jon Winstanley
Keeping track of sessions in a database
跟踪数据库中的会话
If you keep the details of your sessions in a database table and log any activity you can easily check to see which sessions have been inactive for a specific time period.
如果您将会话的详细信息保存在数据库表中并记录任何活动,则可以轻松检查哪些会话在特定时间段内处于非活动状态。
Process Queue
进程队列
Adding clean up functionality to user actions is going to slow down your website unnecessarily. A user should not have to wait for such actions to take place before getting a response from the server.
为用户操作添加清理功能会不必要地减慢您的网站速度。在从服务器获得响应之前,用户不应等待此类操作发生。
CRON
克朗
Instead, you could add the actions to be performed to a queue table, then use a CRONjob to periodically check the queue table to see what needs to be done. The CRON syntax is listed here.
相反,您可以将要执行的操作添加到队列表,然后使用CRON作业定期检查队列表以查看需要执行的操作。此处列出了CRON 语法。
回答by Adrián Navarro
You may store the session id in a database with a time stamp, and update the time stamp each time the user requests a webpage.
您可以将会话 ID 存储在带有时间戳的数据库中,并在每次用户请求网页时更新时间戳。
Then set a cron that looks at every "active" session, calculates the inactivity time and updates the session to "dead" (that may be done with a single SQL statment that runs periodically).
然后设置一个 cron 来查看每个“活动”会话,计算不活动时间并将会话更新为“死”(这可以通过定期运行的单个 SQL 语句完成)。
Also, modify your logout to update the session table and set the session to "dead".
此外,修改您的注销以更新会话表并将会话设置为“死”。
Anyway, I don't think there are other ways to do this (with the "standard" PHP functions), but let's see what other people say.
无论如何,我认为没有其他方法可以做到这一点(使用“标准”PHP 函数),但让我们看看其他人怎么说。
回答by Oliver Friedrich
I had the same problem, though with Lighty+PHP+Ubuntu. I think the problem is, that php does only react on page-requests, so there is no chance to get PHP raise an event on timeout.
我遇到了同样的问题,尽管使用 Lighty+PHP+Ubuntu。我认为问题是,php 只对页面请求做出反应,所以没有机会让 PHP 在超时时引发事件。
You can override the session-store-functionsin PHP but that won't help you either.
您可以在 PHP 中覆盖session-store-functions,但这也无济于事。
You could store the time of last event in the session though, and with every session started clean up old sessions that have run into timeouts. Not a very clean way, but the only working way I found.
不过,您可以将上次事件的时间存储在会话中,并在每个会话开始时清理遇到超时的旧会话。不是一种非常干净的方式,而是我找到的唯一可行的方式。

