-2

I have created a chat system using php and ajax. When a user logs into this chat system, in table is_online field status is updated to 1. I am using this field to list current online members.

I have to update status to 0 if the user closes all the tabs of my website without pressing logout.

How can I do it?

Peon
  • 7,902
  • 7
  • 59
  • 100
open source guy
  • 2,727
  • 8
  • 38
  • 61
  • Take a look at this: http://stackoverflow.com/questions/1619930/how-to-check-users-leave-a-page – Peon Jul 18 '12 at 11:47
  • possible duplicate of [Best way to detect when user leaves a web page](http://stackoverflow.com/questions/147636/best-way-to-detect-when-user-leaves-a-web-page) – JMax Jul 19 '12 at 09:19

4 Answers4

2

THERE IS NO FAIL PROOF, REALTIME, EFFICIENT METHOD

Theoretically speaking there is NO way of safely determining the state of the browser. There are events like DOM onunload or onbeforeunload event. May be you could attach an AJAX request as a response to that event. That will set online=0. But the problem is there is no guarantee that this event will always fire. (Like what happens when the process of the browser is killed?)

The only standard solution is to send periodic very light AJAX requests to the server. The server side program should maintain the lastSeenAtTime field for each user, and automatically set to zero when it does not get the request within some specific time (like 10 minutes may be?)

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

Keep the track of the last message/action in the table. To update the counter select only those that their last action was not more than, lets say, 1 minute ago. And also, if you want, use that is_online with it which would keep the track of logging out :)

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
0

Really bad method.


Make a table online_users with fields name and timeout. Store user's name (or ID, or any other identification) and current timestamp + 5 minutes (or different threshold, you decide).

When displaying online users, select all users from that table where timeout is BIGGER than current timestamp. Example:

$time = time();
$result = mysql_query("SELECT `name` FROM `online_users` WHERE `timeout` > '$time'");

And set up a CRON that will clean the table once a day.

And don't forget to update timeout.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
-1

i think you should check activity of user every 5 minutes for example like yahoo msgr when a user shutdown his computer yahoo shows online status yet.but after 5 minutes its shows offline. its my algorithm .i think you cant check every second and it has delay for your checking time