查找打开的会话数 [英] Find Number of Open Sessions

查看:41
本文介绍了查找打开的会话数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法(无数据库)来列出网站上活跃用户的数量.我想出的最简单的方法是计算打开的会话数.

I'm looking for an easy (no database) method of listing how many users are active on a website. The easiest way I can come up with is by counting the number of open sessions.

此代码应该有效:

$number_of_users = count(scandir(ini_get("session.save_path")));

当然不会因为该目录的安全性限制而存在(应该!).有谁知道在不更改目录权限的情况下访问此号码的另一种方法.

Of course it won't because of the security constraints on that directory (as there should be!!). Does anyone know another way to access this number without changing directory permissions.

注意::我正在寻找一个不涉及数据库的选项或降低PHP会话的安全性.

Note: I'm looking for an option that does not involve databases or reducing the security on PHP sessions.

结束语注::对于遇到此问题的任何人,我最终都从根目录开始使用cronjob(每分钟运行一次),其操作类似于:

End Note: For anyone coming to this question, I ended up using a cronjob (running every minute) from root that did something similar to:

ls /var/lib/php5/ | wc -l > /var/www/sessioncount

确保/var/www/sessioncount文件可被apache用户读取.然后,您可以使用PHP读取文件:

Make sure the /var/www/sessioncount file is readable by the apache user. Then you can just read the file in PHP:

$number_of_users = file_get_contents("/var/www/sessioncount");

推荐答案

在这种情况下,简单并不意味着没有数据库.同样依靠会话来查看有多少用户处于活动状态也不可靠.

Easy does not mean no database in this case. Also relying on session to see how many users are active is not reliable.

如果您想走这条路,您可以每隔几分钟执行一次由安全进程运行的cronjob,并将该计数保存到PHP读取的文件或db中.

If you wanted to go that route, you could do a cronjob that is run by a safe process every few minutes and saves that count into a file or db, which PHP reads.

但是我建议您走数据库路线.

But i recommend going the database route.

这篇关于查找打开的会话数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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