简体   繁体   English

设计问题:如何显示在线用户数?

[英]Design Question: how to show number of users online?

Planning to develop a LAMP web application. 计划开发LAMP Web应用程序。 What general strategies can I use to display the number of users currently logged in to my site? 我可以使用哪些一般策略来显示当前登录到我的网站的用户数? I want to be able to accurately display something like, "There are currently 1000 users online" or "User John Doe is currently online". 我希望能够准确显示“当前有1000个用户在线”或“用户John Doe当前在线”之类的内容。

A database will be involved. 将涉及一个数据库。 So every time someone logs into the site, you can have a field in a user's table for last_login. 因此,每次有人登录该站点时,您都​​可以在用户表中的last_login字段中输入一个字段。 And then there can be a script that does a query against this user's table to count the number of rows last_login within the last x amount of time. 然后可以有一个脚本对该用户的表进行查询,以计算过去x时间内last_login的行数。 It may be good to cache this and repopulate this cache every z amount of time, and then pull from this cache as oppose to running a query against the user's table every request. 最好先缓存此缓存,然后每z的时间重新填充此缓存,然后从该缓存中拉出,以反对对每个请求针对用户表运行查询。 So database + some kind of caching system. 因此数据库+某种缓存系统。

I recommend using the CodeIgniter PHP framework. 我建议使用CodeIgniter PHP框架。

This will allow you to store your session data in the database very easily (you just enable it in the config.php file). 这将使您可以非常轻松地将会话数据存储在数据库中(只需在config.php文件中启用它)。 Then you can query the number of session ids in the session table of your database. 然后,您可以在数据库的会话表中查询会话ID的数量。

Here is the information for the CodeIgniter session class so you can see how to use it: CodeIgniter Session Class 这是CodeIgniter会话类的信息,因此您可以了解如何使用它: CodeIgniter会话类

Here is also a link to the CodeIgniter forums going through more detail of how exactly to get this implemented: CodeIgniter Forum 这也是CodeIgniter论坛的链接,其中详细介绍了如何实现此实现的更多详细信息: CodeIgniter论坛

This is quite easy to do, but cannot be accurate. 这很容易做到,但不准确。 Html being stateless, there is no way of knowing if a user is still looking at your page or has left. HTML是无状态的,无法知道用户是仍在查看您的页面还是已经离开。

If you want to log anonymous and logged in users, you coud use a tracking cookie with a short timeout, say five minutes and have this cookie link to a database of active sessions. 如果要登录匿名用户并登录,则可以使用超时时间短(例如5分钟)的跟踪cookie,并将此cookie链接到活动会话的数据库。

  1. 计算会话数据库中活动会话的数量。

如果您使用数据库来存储会话信息,则可以轻松地查询会话表并获取其中存储了多少个唯一会话。

Have some sort of last accessed time record in the DB, and record that the person has been active. 在数据库中具有某种上次访问的时间记录,并记录此人已处于活动状态。 Then query the DB for those users who have been active in the last 5mins or so. 然后在数据库中查询最近5分钟左右处于活动状态的那些用户。 Will give you a close approx. 会给你接近的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM