Hey. I'm working on a social networking website and I'm struggling to find the best method of checking to see whether a user is online. I would like to put an Online/Offline image on the user's profile which is shown if the user is online or offline, respectively. For example, if the user is browing our website the database is updated with their presence and when they leave the site, the database is updated so that the user_is_online field is set to false. This is also important for the admin panel so we can have a list of all members that are currently browing the website. Could someone please direct me to some information of how to do this, please? Thanks. Ryan.
a better solution might be a "last_seen" field that has like a tolerance for 15 minutes. Alternatively, if you also want to display everyone online, you can make a whosonline table in which you save the userid and last_seen time Since you do want a list of users online (granted, you could also do this by just searching the last_seen times in the user table - but that would be heavier on the server), I'd go with the latter. Just have a table that is updated whenever the user opens a new page, and users older than 15 minutes are deleted. This is also very handy if you want to display to your users what's new - because you can just save their last seen time once they get deleted from the who's online table, and just mark everything created after that as new. If you do that, it's important that you don't update it before deleting them from the online table (again it should have a tolerance! for like 15 minutes or so)
Or just update that table only once every 15 minutes. If you have a popular site, it could otherwise be problematic if you use myisam. If you use kyosys approach you may want to switch that 1 table to innodb so that row level locking is supported.