简体   繁体   English

如何统计在线用户?

[英]How to count online users?

I am trying to count users that are now online, so I made these 2 queries我正在尝试计算现在在线的用户数,因此我进行了这两个查询

When logging in登录时

$id = $_SESSION['id'];
$up = mysqli_query($conn,"UPDATE user_account SET status= 1 WHERE id='$id'");

When logging out退出时

$id = $_SESSION['id'];
$up = mysqli_query($conn,"UPDATE user_account SET status= '0' WHERE id='$id'");

0 value is for offline while 1 is for online so it works fine, but I tried this on incognito browser mode I logged in to an account then I closed the incognito browser mode and the seassion id still exists and never turn 0 is there is way I can prevent that? 0值用于离线,而1用于在线,所以它工作正常,但我在隐身浏览器模式下尝试了这个我登录到一个帐户然后我关闭了隐身浏览器模式并且session id仍然存在并且永远不会变成0有办法我能阻止吗?

Your server can't ever know what a user does unless the user tells the server.除非用户告诉服务器,否则您的服务器永远无法知道用户在做什么。 For this reason people implement session timeouts.出于这个原因,人们实现了会话超时。 If there was no activity from the user for a certain period of time you assume they walked away.如果用户在一段时间内没有活动,您就假设他们走开了。

You can create additional column in your database table called last_active of type DATETIME and you would update it each time a user makes a request to the server.您可以在数据库表中创建名为last_active的 DATETIME 类型的附加列,并且每次用户向服务器发出请求时都会更新它。 If you want to count active users you simply make a query to fetch users active recently.如果您想计算活跃用户数,您只需进行查询以获取最近活跃的用户。

// Do this every time a user makes a request to server
$stmt = $conn->prepare('UPDATE user_account SET last_active=NOW() WHERE id=?');
$stmt->bind_param('s', $_SESSION['id']);
$stmt->execute();

// To fetch count:
$count_of_active_user = $conn->query('SELECT COUNT(*) FROM user_account WHERE last_active >= NOW() - INTERVAL 15 MINUTE')->fetch_row()[0];

Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries.警告:您对SQL 注入持开放态度,应该使用参数化的准备好的语句,而不是手动构建查询。 They are provided by PDO or by MySQLi .它们由PDOMySQLi 提供 Never trust any kind of input!永远不要相信任何类型的输入! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data .即使您的查询仅由受信任的用户执行,您仍然存在损坏数据的风险 Escaping is not enough!逃避还不够!

Logically you can never know if a user closed a browser (whether it is incognito or normal mode), turned off computer, or any other possibilities.从逻辑上讲,您永远无法知道用户是否关闭了浏览器(无论是隐身模式还是正常模式)、关闭计算机或任何其他可能性。 You only turn the status off when the users normally logs out using the button in your site and none of the other ways are being counted.只有当用户通常使用站点中的按钮注销并且其他方式都没有计算在内时,您才关闭status

One way is to work with all sessions.一种方法是使用所有会话。 You can find some info here .你可以在这里找到一些信息。 This way also does not turn off the status immediately.这种方式也不会立即关闭status

Best way:最好的办法:

But I recommend you to use JavaScript on your site to tell the server that the user is online, each minute for example, (or any time the user interacts with the site which includes any request to the sever).但是我建议您在您的站点上使用 JavaScript 来告诉服务器用户在线,例如,每分钟(或任何时间用户与站点交互,包括对服务器的任何请求)。 And set a DATETIME value in database to store the last activity of each user on the site, so you can set a limit and say for example if it's more than 5 minuets from a user's last activity the the user is offline, else he/she is online.并在数据库中设置一个DATETIME值来存储每个用户在站点上的最后一次活动,因此您可以设置一个限制,例如,如果距离用户的最后一次活动超过 5 分钟,则该用户处于离线状态,否则他/她在线。

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

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