我可以计算会话次数来确定在线人数吗? [英] Can I count sessions to determine number of people online?

查看:80
本文介绍了我可以计算会话次数来确定在线人数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对某些事情感到困惑.当我尝试搜索如何用PHP计算在线用户时,所有答案都与MySQL和许多不同方式有关.

I am confused about something. When I try to search how to count online users in PHP, all answers related with MySQL and many different ways.

在我的脚本中,任何提交登录表单的用户都会创建一个$ _SESSION ['$ nickname']

In my script, any user that submits the login form creates a $_SESSION['$nickname']

所以我想,我可以用count($ _ SESSION ['$ nickname'])来计算登录会话数吗?并显示在我的页面中?

So I thought, can I count login sessions with count($_SESSION['$nickname']); and show it in my page?

或者这完全是错误的逻辑吗?

Or is this totally a wrong logic?

推荐答案

完全错误的逻辑. $ _SESSION是每个用户的东西.一个用户的会话未与任何其他用户的会话共享.考虑一下-用PHP编写的在线银行,都共享一个$ _SESSION-每个人都会看到每个人的帐户详细信息.

Totally wrong logic. $_SESSION is a per-user thing. One user's session is not shared with any other user's session. Think about it - an online bank written in PHP, all sharing a single $_SESSION - everyone would see everyone's account details.

假设您正在使用标准的基于PHP文件的会话,则可以将会话文件计入它们存储的任何目录中,例如

Assuming you're on the standard PHP file-based sessions, you can count the session files in whatever directory they're stored, e.g.

$users = count(glob(session_save_path() . '/*'));

请注意,这仅计算会话文件-毫无疑问,它将包含尚未被垃圾回收的陈旧/死会话.如果您希望实际的现在真的在线",则必须解析每个会话文件并检查其内容.

Note that this just counts session files - it will undoubtedly contain stale/dead sessions that haven't been garbage collected yet. If you want an actual "really is online right now", you'd have to parse each session file and examin its contents.

这篇关于我可以计算会话次数来确定在线人数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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