简体   繁体   中英

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.

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. Every page in my web-app uses a javascript wich makes a get-request every minute to a certain php-file. 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. If there are timestamps older than 2 minutes you can mark the user as "logged out". 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.

This solution works well, but uses javascript timers. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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