1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
mintuz
  • 735
  • 1
  • 18
  • 40

4 Answers4

5

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.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
4

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

Community
  • 1
  • 1
Bjoern
  • 15,934
  • 4
  • 43
  • 48
2

You can check for user are "answering" by Ajax for example. Or you can set status offline by inactivity timeout.

triclosan
  • 5,578
  • 6
  • 26
  • 50
0

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.

ren
  • 3,843
  • 9
  • 50
  • 95