简体   繁体   English

脚本告诉我谁和有多少用户在线

[英]script to tell me who, and how many users, are online

In my research to find a way to make PHP tell me how many people are 'online' on my site I've discovered that there are ways to 'estimate' this. 在我的研究中,为了找到一种方法让PHP告诉我在我的网站上有多少人“在线”,我发现有很多方法可以“估计”这个。

I've chosen to log everything that happens on the site, also for error-management sake, but now i'm stuck at writing my SQL query. 我已经选择记录网站上发生的所有事情,也是出于错误管理的缘故,但现在我一直在编写我的SQL查询。

Basicly I have a database with 'IP', 'userid' and 'datetime' and I figured that a query like this would do the trick: 基本上我有一个带有'IP','userid'和'datetime'的数据库,我认为像这样的查询可以解决这个问题:

SELECT distinct(IP), datetime 
FROM `bigBrother` 
WHERE datetime BETWEEN DATE_SUB(NOW(), INTERVAL 3 MINUTE) AND NOW()

The problem is that my site is mostly viewed and used by students on the school network, and well... they all have the same IP. 问题是我的网站主要由学校网络上的学生查看和使用,而且......他们都拥有相同的IP。

So the question is, am I doing this right, and can I select two distinct rows from my database, so that I can sort out the registered users (who will have a 'userid' - others will have userid = 0)? 所以问题是,我这样做是否正确,我可以从我的数据库中选择两个不同的行,以便我可以整理注册用户(谁将拥有'userid' - 其他人将拥有userid = 0)?

只需使用会话ID而不是IP。

Use cookies instead of IP addresses. 使用cookie而不是IP地址。

PHP makes it very easy with it's session mechanism. PHP使用它的会话机制非常容易。 On each you first do a session_start() and then you use the value returned by session_id() as a identifier of the visitor that you can put in your database. 在每个上,首先执行session_start() ,然后使用session_id()返回的值作为可以放入数据库的访问者的标识符。

I created a system for a school site that asked for this feature as well and here's how i did it. 我为学校网站创建了一个系统,该系统也要求这个功能,这就是我如何做到的。

I had a table of users, in that table there was a field called "online_time" 我有一个用户表,在该表中有一个名为“online_time”的字段

On every page a function was called if the user was logged in that updated the "online_time" to the current time of that user. 在每个页面上,如果用户已登录,则会调用一个函数,将“online_time”更新为该用户的当前时间。 (unixtime) (unixtime)

Then i had an "Who is online" function that looked at the "online_time" and displayed all the users with the online time of the last 5 minutes. 然后我有一个“谁在线”功能,查看“online_time”并显示所有用户在过去5分钟的在线时间。

EDIT to the comment: 编辑评论:

You could make the same function save the session id in another table and the time it was saved. 您可以使用相同的功能将会话ID保存在另一个表中以及保存时间。 The session id is unique to that user browsing, so you could get the number of session id's active within the last 5 minutes. 会话ID对于该用户浏览是唯一的,因此您可以在最后5分钟内获得会话ID的活动数量。

session_id()

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

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