开发者

Check what users are online

I have a PHP $_SESSION variable with a users name. I have a MYSQL database with a unique field for the users name. How can I keep the db up开发者_如何学编程 to date with names of online users, as well as removing them when they leave. Is there some way I could do it with JQuery.ajax?


The way I do it is periodically ping a special handler using AJAX (once in 30 seconds). That's a whole lot of requests, so instead of updating the online users table each time with last-visit I'm updating memcache. I only update the database record when it's 5 minutes behind memcache record (you can pick a larger or smaller delay depending on the load). Then a cron job removes stale records from online users table.

To check if a specific user is online I simply check his memcache record. The error is never more than 30 seconds. The database is never more than 5 minutes behind, so the results from the database are pretty accurate too.

I also use these periodical requests to push events to the user.


For sign in users, you can poll the requests to server, update timestamp. After a fixed interval of time, say 5 minute, you have a cron job that deletes the records from online user table whose timestamp is older than 5 minutes.

In case if user log out, you can directly remove entry from online users table. To do more accuracy, you can decrease interval to 2 minutes.


The easiest way to determine when I user comes online, is during login. You could add a column to your users table that is called something like last_login or last_seen and update that column the moment the user successfully logs in.

Unless the user explicitly logs out, it´s going to be very hard to determine whether a user is still online. If you don´t mind the extra database writes, you could update the last_seen column every time a page loads and you do your login check. Then you'd have to set a reasonable time (15 minutes, 30 minutes?) as the time the user still might be there.

You could use ajax and events like window close or other ones where the user leaves the page to update a column that says something like last_logout but I would not rely on that as:

  • the user can have multiple windows / tabs open
  • the user might have javascript disabled
  • the user might have lost internet connection
  • etc.


The simplest way is that you create MySQL MEMORY table where you should add/update records on each new request (for each user). Unique key for table should be session ID and you should also have timestamp column (on update/insert). And if you want you can add userID column in case that you need some info about user (username in your case) Then use cron job to delete all records older then for example 5 minutes. You can change this value of course but run cron every minute. Why memory table? Because it is fast (increase memory usage a bit) so you will not feel the difference and in case of server restart it is cleared automatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜