To check online users

Hello All,

I was searching for something which could tell me that whether a user is online or not. I got this idea.

all you need is to create another two fields in table with users: online (tinyint) and last_online (timestamp).
Each time (or each second time) user enters on the page you update the last_online to now() and set online to 1, like that:
UPDATE users SET last_online = NOW(), online = 1 WHERE id = $user_id
And execute another SQL statement to put offline users who are not online:
UPDATE users SET online = 0 WHERE online = 1 AND last_online < NOW() - 120

I am not clear with the logic of last line : UPDATE users SET online = 0 WHERE online = 1 AND last_online < NOW() - 120
. How this will check whether the user is still online or not. What if he may be online but our query turns it to offline.


since the web uses HTTP, which is a stateless protocol, the meaning of online is a little bit vague. I could be for an hour on a page without contacting the server and I can discard a page I just loaded.

so there is nothing that tells you whether a page is actually being viewed (and be it if the user is out there making coffee). the best you can do is log the last time a user requested a page and assume that he is on that for a given amount of time.

1 Like

Ok… So is that the same way which gmail, fb or other sites are using?

This website also shows the number of users viewing a certain category… How they manages to do it

assumption based on the last request.

120 is in seconds?

Or some kind of JavaScript ‘heart beat’ that pings the server at set intervals. I know that still doesn’t tell you if a user is AFK, but at least you know if the page is still open.

1 Like

Means everything works on assumptions…
Actually, i want to show a online/offfline icon on a user profile. There may be a condition where he will be online and will see his status offline, if i fix it to certain interval.

used this and successful. Worked god for me/… Thanks Alot