I am writing a php app and i log users login and logout time in a txt file. Login is the easiest part. Logout is a little tricky i think. When users session is killed manually by selecting logout choice, i can track logout time without second thought. But what can i do when he closes his browser’s tab or window? I think i have to use js to detect those events with window.onbeforeunload=”foobarA()” window.onunload=”foobarB()” and then trigger php to track time to txt as logout time. But this checks only browser’s window close effect. If user closed a tab that our app was running and then was playing or surfing around internet for hours without closing browser, how can i track precise his logout time?
Share
You can’t rely on unload events as a) not every browser will handle them and b) different browsers have different restrictions on what can be done in them. You could use a websocket and monitor when the connection gets closed from your server. For clients that don’t support websockets, you can post-back using AJAX to your script every 5 seconds. The have “logged out” when you haven’t received a request in the last 5s.