1.将count.php文件放到网站根目录,内容如下
<?
//创作来自亳州淮中网:http://www.guoyangnet.cn使用请保留版权。
session_start();
$max_len = 8;
$CounterFile = "counter.dat";
if(!file_exists($CounterFile))
{
$Counter = 0;
$cf= fopen($CounterFile,"w");
flock($cf,3);
fputs($cf,"0");
fclose($cf);
}
else
{
$cf = fopen($CounterFile,"r");
flock($cf,3);
$Counter = trim(fgets($cf,$max_len));
fclose($cf);
}
if (session_is_registered("in")==false){
$Counter++;
$cf = fopen($CounterFile,"w");
flock($cf,3);
fputs($cf,$Counter);
fclose($cf);
session_register("in");
}
$strCount = strval($Counter); //获得的记数转成字符串
echo "document.write(\"".$strCount."\");";
?>
2.在需要显示网站访客数量的地方加入下面的代码
<script src="/count.php"></script>
原文:http://www.cnblogs.com/zhouyx/p/6677484.html