I have been trying all day to figure out how to tell which of my users is online and I have not been able to wrap my head around it. I have created a custom login system with all the usual fields (username, password, email, ip, etc) and I really want to be able to display who is on the website at any given time. The preferable option would be to have a real-time display of this showing, but if it is significantly easier then displaying who has been online in the last 30 minutes (time it takes for the session to expire) would be fine as well. Either way, I haven't been able to figure out how to check for connections to my website.
Add a column to your db structure for e.g. 'online' Then on your login page simply do an UPDATE query setting the 'online' to 1, and then on your logout page simply do an UPDATE query setting the 'online' to 0. Now you can check whos online by using the WHERE clause (WHERE online = 1)
Since users aren't required to login when their session or cookie is still active, then that would say they are online potentially indefinitely which is not really helpful. There has to be some way to do it since there are tons of cms scripts that have features like this, but I just don't know how.
I usually add a column 'lastAction' and update it with a timestamp everytime the user loads a page. Then as long as the users' lastAction is within say 5 minutes they are considered online. When the user logs off I update the lastAction to 0 or anytime earlier than 5 minutes. I know thats not the most efficient way to do it, but I haven't found a better way yet. Cheers!
That is definitely a viable option, but if possible I'd like to avoid running a query on every page load.
You could use that option along with a cookie/session with the timestamp, and only query an update every time it expires. That way if the user loads a page after the 5 minutes they will stay online.
I'll work on trying to find little way's here and there to limit the number of queries that need to be run, but thanks for the overall idea. So far it's working perfectly.