简体   繁体   English

PHP + MYSQL,需要每n分钟更新一次DB中的注销时间

[英]PHP + MYSQL, Need to update logout time in DB every n minutes

当用户通过关闭浏览器或重新启动PC注销时,如何在数据库中保存注销时间?

If they are not physically clicking the log-out button (where you are recording their log-out time), you should set the log-out time to the session expiration. 如果他们没有实际单击注销按钮(在其中记录其注销时间),则应将注销时间设置为会话到期。 In order for this method to be more accurate, decrease the lifetime of the session to a couple of minutes. 为了使此方法更准确,请将会话的生命周期减少到几分钟。 Thus, after a couple of minutes of inactivity, the session will expire and your log-out time will be relatively close to when the user stopped using your software. 因此,在闲置几分钟后,会话将终止,并且您的注销时间将相对接近于用户停止使用您的软件的时间。 See below on how to record this information. 请参阅以下有关如何记录此信息的信息。 Cron job or javascript implementation is necessary. Cron工作或javascript实现是必要的。

Just my two cents... probably better ways to do this, but this way seems to me as the easiest. 只是我的两分钱...可能是更好的方法,但是在我看来,这种方法是最简单的。

I managed it with a kind of user-ping. 我通过一种用户ping管理它。 Every page in my web-app uses a javascript wich makes a get-request every minute to a certain php-file. 我的网络应用程序中的每个页面都使用JavaScript,每分钟都会向某个php文件进行get-request。 This file identifies the user and updates a last-activity timestamp in my user table. 该文件标识用户并更新我的用户表中的上次活动时间戳记。 On the server I made a cronjob that triggers every 2 minutes and checks the usertable for last-activity timestamps that are older than 2 minutes. 在服务器上,我做了一个cronjob,每2分钟触发一次,并检查usertable中是否有2分钟以上的最新活动时间戳。 If there are timestamps older than 2 minutes you can mark the user as "logged out". 如果时间戳记早于2分钟,则可以将用户标记为“已注​​销”。 This way the user can spend long time on one page and won't get logged out due to inactivity. 这样,用户可以在一页上花费很长的时间,并且不会因不活动而注销。 If he closes the browser or restarts the pc the logout timestamp will be set by the cronjob and is pretty exact. 如果他关闭浏览器或重新启动PC,则登出时间戳将由cronjob设置,非常准确。

This solution works well, but uses javascript timers. 此解决方案效果很好,但是使用了JavaScript计时器。 It is not really nice, but makes it's job pretty fine. 它不是很好,但是可以很好地工作。 If your page allready uses a lot of other javascript you perhaps should use a other solution. 如果您的页面已经准备好使用许多其他javascript,则您可能应该使用其他解决方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM