First this is related question of PHP: Online Offline Status But as my approch is little different and issue too so I thought new question would be better.
Now I ha
Let me start with this:
Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
The approach I used for this sort of thing, is to not only save the logged in status of the user, but to save his last activity time as well.
Where activity may mean:
A user is considered logged out if one of the following apply:
To check activity, you compare the last activity time against the current time. If the difference ($currentTime - $lastActiveTime) is greater than N, you consider the user as logged out.
The list of logged in users would be refreshed every couple of seconds (using a caching mechanism of some sort), where Inactive users would be UPDATEd to "Logged Out" in the database, thus saving you some query time and later processing time.