问题描述
我正在尝试对一个开源项目进行一些更改.我想跟踪用户何时登录和注销.现在,当他们登录或手动注销时,我在db中更改了他们的登录状态.现在的问题是,我无法确定用户是否只是在不按注销按钮的情况下关闭了浏览器.因此,我需要触发一个函数,该函数将在每次用户会话期满时更改数据库.我已经在PHP中尝试了session_set_save_handler
,但是看起来我需要重写整个会话行为.我正在寻找的是保留默认的会话行为,并且仅在用户会话过期时添加功能.有办法吗?
I am trying to make some changes to an opensource project. I want to keep track of when users log in and log out.Right now I change their login status in db when they login or manually log out. The problem right now is that I cannot find out if the user just closed their browser without pressing on logout button.For this reason I need to trigger a function that will change database every time the user's session expires.I've tried session_set_save_handler
in PHP, but it looks like I need to override the whole session behavior. What I am looking for is to keep default session behavior and just add functionality when the user's session expires. Is there a way to do that?
推荐答案
我确实做了确实讨厌的一次.每次通过页面刷新/访存等更新"会话时,我都会更新数据库行上的时间戳.第二个守护程序每10分钟轮询一次数据库,并执行清理"操作.
I did something really nasty once. Every time a session was "updated" by a page refresh / fetch / etc., I updated a timestamp on a DB row. A second daemon polled the DB every 10 minutes and performed "clean-up" operations.
您将找不到任何本地PHP设施来实现您的目标.会话超时不在后台运行.您甚至都不知道会话是否超时,除非会话超时会尝试其他访问.此时,几乎无法陷井,您可以下定决心并进行适当处理.
You won't find any native PHP facilities to achieve your goal. Session timeout doesn't run in the background. You won't even know if a session is timed out, unless a timed out session attempts another access. At this point, nearly impossible to trap, you can make your determination and handle it appropriately.
我建议队列&轮询此问题的体系结构.这很容易,而且肯定会工作.如果您担心事务性能,请添加memcached.
I'd recommend a queue & poll architecture for this problem. It's easy and will definitely work. Add memcached if you have concerns about transaction performance.
这篇关于PHP会话过期事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!