检查哪些用户在线 [英] Check what users are online

查看:78
本文介绍了检查哪些用户在线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有用户名的PHP $ _SESSION变量.我有一个MYSQL数据库,其中的用户名具有唯一字段.如何使数据库与在线用户的名称保持最新,以及在他们离开时将其删除.有什么办法可以用JQuery.ajax做到吗?

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?

推荐答案

我的方法是使用AJAX定期ping一个特殊的处理程序(一次30秒).这是大量的请求,因此不是在每次上次访问时都更新在线用户表,而是更新了内存缓存.我仅在数据库记录比Memcache记录晚5分钟时更新数据库记录(您可以根据负载选择更大或更小的延迟).然后,cron作业会从在线用户表中删除过时的记录.

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.

要检查特定用户是否在线,我只需检查其内存缓存记录即可.错误永远不会超过30秒.数据库永远不会超过5分钟,因此数据库的结果也非常准确.

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.

这篇关于检查哪些用户在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆