个人网站使用PHP统计页面显示IP总访问量、本月访问量、昨日访问量、今日访问量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

<?php
$file = dirname(__FILE__).'/tongji.txt';
//$data = unserialize(file_get_contents($file));
$fp=fopen($file,'r+');
$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;
echo "总访问量:{$total}, 本月访问量:{$month}, 昨日访问量:{$yesterday}, 今日访问量:{$today}";
//echo "document.write('总访问量:{$total}, 本月访问量:{$month}, 昨日访问量:{$yesterday}, 今日访问量:{$today}');";
}
?>

🔰本文标题: 个人网站使用PHP统计页面显示IP总访问量、本月访问量、昨日访问量、今日访问量

🔞本文链接: https://1han.wiki/PHP-IP-TongJi/index.html

🌡️本文总热度