setting value if user exits website
heres a issue i have. When a user logs in on the website, it sets a value to indicate they are offline. If they logout through the开发者_如何学运维 website, the value is set to indicate the user is offline.
But if the user just closes the website without pressing logout, it still indicates they are online.
How can i make it so it makes them offline once they have closed the website.
my website is using php, html, css and mysql.
The most common approach is to save a timestamp with the user's last activity instead of just an "online" flag. Update the timestamp on every activity and calculate offline users by checking for users which have been inactive for more than, say, five minutes.
For performance reasons you may want to save the timestamp into the users current session as well and only update your activity timestamp in the database when it is about to expire.
Since closing a browser (or a browser tab) doesn't fire any events to your server, basically you can't react to this. In such a case I'd prefer a heartbeat mechanism.
Another way is to "assume" the client has logged out if he hasn't fired any event since lets say 20mins or so.
A similar issue has been discussed here: Check if user is offline
You can check for user are "answering" by Ajax for example. Or you can set status offline by inactivity timeout.
perhaps there is some javascript event when browser closes, on which you could using ajax send notification to the server.
A better approach i would guess is to have client's javascript to periodically notify server that user is still there. Once notification is not received - he must be offline.
精彩评论