如何在PHP中学习在线用户号? [英] How to learn online user number in PHP?

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

问题描述

我正在建立一个论坛,我需要计算在线人数。

I'm developing a forum and I need to count the number of online persons.

根据与之相关的旧问题,他们说 count()函数。

According to old questions which are related to it, they says the count() function should be used.

但是 count()函数仅计算特定用户的会话变量。
我的意思是在codeigniter中, count($ _ SESSION ['user'])给出数字 2 ,数字指向 $ _ SESSION ['user'] ['name'] $ _ SESSION ['user'] ['group']

But the count() function counts just specific user's session variables. I mean in codeigniter, count($_SESSION['user']) gives number 2 and this number points to $_SESSION['user']['name'] and $_SESSION['user']['group'].

因此,每个用户在屏幕上都会将数字 2 视为在线人数。但是我需要服务器中分配的 $ _ SESSION [’user’] 变量的数量,而不是特定会话变量的子变量的数量。解决办法是什么?如何检测在线人数?

So every user sees number 2 in their screen as online person number. But I need to number of allocated $_SESSION['user'] variable in the server, not number of specific session variable's sub variables. What is the solution? How can I detect number of online persons?

推荐答案

在这种情况下,您希望拥有一个数据库,最好是 MySQL 并在用户每次重新加载 CURRENT_TIMESTAMP()时更新 users 表页面或在网站上进行任何操作。

In this case, you want to have a database, preferably MySQL and update the users table with CURRENT_TIMESTAMP() whenever the user reloads the page or does anything on the site.

此后,如果要在线获取当前用户,则可以

Afterwards, if you want to obtain current users online, you can do

SELECT COUNT(*) FROM users WHERE last_activity > NOW() - INTERVAL 5 MINUTE;

这篇关于如何在PHP中学习在线用户号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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