PHP观察者模式可在会话超时时注销用户 [英] php observer pattern to log user out when session times out

查看:82
本文介绍了PHP观察者模式可在会话超时时注销用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户的会话超时发生时注销用户.注销用户-就我而言-需要修改数据库中用户的在线"状态.
我当时在想,我也许可以使用观察者模式进行一些操作,以监视用户会话的状态并在会话过期时触发回调-这将保留用户名,以便我们更新数据库.我不确定在会话端从哪里开始.我可以将回调与会话的超时联系起来吗?

I'm trying to log users out when the user's session timeout happens. Logging users out - in my case - requires modifying the user's "online" status in a database.
I was thinking that I might be able to use the observer pattern to make something that would monitor the state of the user session and trigger a callback when the session expires - which would preserve the user's name so we can update the db. I'm not exactly sure where to begin on the session side. Can I tie a callback to the session's timeout?

这些东西是否内置在任何可用的pear或zend会话程序包中?我将尽我所能实现这一目标!

are these things built into any available pear or zend session packages? I will use whatever I have to to make this happen!



更新@ 16:33:
如果您拥有一个用户可以彼此交互(但他们只能与在线用户交互)的系统怎么办?用户需要知道当前还有哪些其他用户在线.



UPDATE @ 16:33:
What if you have a system where users can interact with each other (but they can only interact with online users)? The user needs to know which other users are online currently.

如果我们只是在每次刷新页面时检查会话是否仍处于活动状态,则在超时后,用户将被发送到未登录页面,但在系统中仍被列为在线用户.

If we simply check to see if the session is still alive on each page refresh, then after a timeout, the user is sent to a non-logged in page, but they are still listed as online in the system.

该方法很好,除了在会话超时时会丢失有关用户的信息外,这些信息可用于注销用户.

That method would be fine except that when we timeout the session, we lose the information about the user which could be used to log them out.



更新@ 16:56:
正确的.谢谢.我同意...有点丑陋.我已经对服务器进行了一些缓慢的轮询,因此实现该方法将非常容易.对于会话处理包来说,这似乎是一个有用的功能. Zend和PEAR都有会话包.



UPDATE @16:56:
right. Thanks. I agree...sort of ugly. I already have some slow polling of the server happening, so it would be quite easy to implement that method. It just seems like such a useful feature for a session handling package. Zend and PEAR both have session packages.

推荐答案

每当用户点击页面时,在数据库中标记该时间,就将该列称为LastAccessed.当用户单击站点的注销"部分时,可以将此值设置为null.在编写查询以查找当前登录用户的列表时,请执行以下操作:

Whenever a user hits a page, mark that time in the database, call this column LastAccessed. When the user clicks on the Logout portion of your site, you can set this value to null. When writing your query to find a list of users who are currently logged in, do the following:

SELECT *  FROM Users WHERE LoggedIn=1 AND LastAccess > DATEADD(Minute,-20.GETDATE())

这将返回仍处于活动会话的用户.请原谅可能不适用于MySQL/PHP的SQL,但这应该为您提供了一个大致的概念.

Which would return the users who still have an active session. Pardon the SQL which probably doesn't work with MySQL/PHP, but this should give you a general idea.

这篇关于PHP观察者模式可在会话超时时注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆