PHP-访问者在线柜台 [英] PHP - Visitors Online Counter
问题描述
我有以下代码来统计PHP网站上的访问者。在使用WampServer的本地开发计算机上,它可以正常工作,但是当我将文件上传到托管帐户进行测试时,我意识到它无法正常工作。
I have the following code to count visitors on my PHP site. It works fine on my local development machine using WampServer but when I uploaded my files to my hosting account for testing I realized that it does not work properly.
我的满意度很高数字计数,并且还注意到会话从未删除,因此它们只会不断累积。
I get really high number count and also noticed the session are never deleted so they just keep accumulating.
这是一个简单的会话计数器。有更好的方法吗?可以请一个人给我看一下还是指点什么? 谢谢!
It is a simple session counter. Is there a better way of doing it? Could some one please show me or point me to some article? Thank you!
<?php
//------------------------------------------------------------
// VISITORS ONLINE COUNTER
//------------------------------------------------------------
if (!isset($_SESSION)) {
session_start();
}
function visitorsOnline()
{
$session_path = session_save_path();
$visitors = 0;
$handle = opendir($session_path);
while(( $file = readdir($handle) ) != false)
{
if($file != "." && $file != "..")
{
if(preg_match('/^sess/', $file))
{
$visitors++;
}
}
}
return $visitors;
}
?>
推荐答案
如果您想要自己的内部计数系统,那么我会建议,将与网站有关的信息存储在数据库中。并在用户每次浏览网站时更新记录。
If you want your own internal counting system then I would suggest, storing such information related to website in database. And update the record everytime a user browses the website.
这篇关于PHP-访问者在线柜台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!