标签搜索

php访客量统计

沐雨酆臻
2024-03-08 / 0 评论 / 7 阅读 / 正在检测是否收录...

下面写一个简单的php访客量统计

随便创建一个php文件,例如:tonji.php 然后在文件里写入 访客统计的php代码


//设置时区为上海
date_default_timezone_set("Asia/Shanghai");
//自动获取当前文件夹 可以更改为你喜欢的名字
$file = dirname(__FILE__).'/tongji.db';
//使用a+ 当文件不存在时会自动创建并写入
$fp=fopen($file,'a+');
$content='';
if (flock($fp,LOCK_EX)){
while (($buffer=fgets($fp,1024))!=false){
$content=$content.$buffer;
}
$data=unserialize($content);
//设置记录键值
$total = 'total';
$month = date('Ym');
$today = date('Ymd');
$yesterday = date('Ymd',strtotime("-1 day"));
$tongji = array();
// 总访问增加
$tongji[$total] = $data[$total] + 1;
// 本月访问量增加
$tongji[$month] = $data[$month] + 1;
// 今日访问增加
$tongji[$today] = $data[$today] + 1;
//保持昨天访问
$tongji[$yesterday] = $data[$yesterday];
//保存统计数据
ftruncate($fp,0); // 将文件截断到给定的长度
rewind($fp); // 倒回文件指针的位置
fwrite($fp, serialize($tongji));
flock($fp,LOCK_UN);
fclose($fp);
//输出数据
$total = $tongji[$total];
$month = $tongji[$month];
$today = $tongji[$today];
$yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;
//设为js输出 如果你想直接输出就把 document.write()删掉
echo "document.write('      总访问量: {$total}
本月: {$month}昨日: {$yesterday}今日: {$today}');"; }
0

评论 (0)

取消