There are several ways to do this, depending on how accurate you are.
The easiest and first way to do this is to use memecached or a permanent store to track users, and the last time they hit the page. Think about who got on the page for X minutes to log in if they did not "log out". If you just refresh db on every page hit or refresh memcached (or faster db) then request it for sessions in the last minute.
Secondly, but more intensively on your server, put Javascript on every page that gets to your siteβs special URL every XX seconds / minutes to indicate that the user is logged in. Write it down and use this account. This has the advantage that your JS can maintain a counter on the current page in real time. Although polling can be expensive on your server.
Or, thirdly, use the Comet style system, say, with Orbited, and while the user is on the page connected to the server, the connection will be open and you can track how many open sessions you have. Orbited can handle about 10 thousand open connections at a time on one server, which, I believe.
I would recommend the first type, since it requires the least additional overhead / setup and handles a fairly common case. If you use real-time chat where you need better accuracy, consider one of the other two.
Ben bangert
source share