4

I have an online game. I wish to show how many user are online. The problem is to know when a user is offline. Is there a way to perform a check on sessions cookie to acknowledge whether the session with the broswer was closed? I was thinking about simply set a timeout on the server which launch a script that count how many session cookie are present, but how do I check if the session cookie is about somebody who's logged and not just a visitor?

How did you handle this?

1) I don't want to rely on a script fired with the logout button, since nobody ever logout... people simply close the browser.

2) About timestamps and registering activity? Since in my game users interact with an svg (not moving through pages), they generate a huge amount of clicks. Making a query for each click for each of them refreshing a record would be very expensive.

Damiano Barbati
  • 3,356
  • 8
  • 39
  • 51

2 Answers2

4

When the user interacts with the site, set their last activity time.

If it is longer than 30 mins or so, you can assume they are offline.

You can also explicitly set someone to offline when they click logout.

However, your case is a little different. You could use a heartbeat style script.

Whilst they are on the page, use setInterval() to extend the expiry date, up to a maximum range (in case the user leaves their browser window open for hours on end).

alex
  • 479,566
  • 201
  • 878
  • 984
  • Thanks for replying, but as I said: I have an online game based on an svg board. User can close the main page, and play on the svg page... but they click something like 50 times in 2 minute in each game. Updating the db for each click is heavily expensive for me. – Damiano Barbati Apr 12 '11 at 11:10
  • this is kind of "keepAlive", the user will still be online, no matter that he has no activities – Teneff Apr 12 '11 at 11:13
  • 2
    @hysoka44 I didn't say update database on every click, I said to use an interval to *phone home* which will extend the expiry date. Of course this is never going to be 100% accurate, but it should be close enough without making any more tradeoffs. – alex Apr 12 '11 at 11:16
  • interesting. I could set an interval which phone a php script which just session_cache_expire(something), so the session will be prolonged. Is that what u meant? But how do I check how many logges essions are present on the server after all? – Damiano Barbati Apr 12 '11 at 11:30
  • @hysoka44 Do something like (assuming MySQL) `SELECT COUNT(\`id\`) FROM \`users\` WHERE \`expiry\` > NOW()`. – alex Apr 12 '11 at 11:33
  • but this is valid if I'm using sessions based on database, and that imply that I have to manually modify the expiry date of the user session. It's the same thing using the timestamp method. Am I missing something? – Damiano Barbati Apr 12 '11 at 12:32
2

Since your code gets executed when the page is loaded you cannot make a check if the user closed his browser or not.

So the common approach would be to use timestamps and update this stamp if the user does something on your site and if the timestamp is older than say 5 minutes you just assume he is offline

Chris
  • 2,030
  • 1
  • 16
  • 22
  • Yep, I know. But... Since in my game users interact with an svg (not moving through pages), they generate a huge amount of clicks (one every click). Making a query for each click for each of them refreshing a record would be very expensive. – Damiano Barbati Apr 12 '11 at 11:34