为thinkphp网站添加昨日今日总访问统计代码
2024-10-11 12:29:05 建站经验 thinkphp网站添加昨日今日总访问统计代码
实现 总访问 , 本月访问, 昨日访问, 今日访问统计,使用文本存储,简单好用 自动创建文本数据库,保存本php同目录。显示效果如图:
- error_reporting(0);
- header ( "Content-type: text/html; charset=utf-8" );
- //设置数据文件路径
- $file = dirname(__FILE__).'/tongji.db';
- $data = unserialize(file_get_contents($file));
- //设置记录键值
- $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];
- //保存统计数据
- file_put_contents($file, serialize($tongji));
- //输出数据
- $total = $tongji[$total];
- $month = $tongji[$month];
- $today = $tongji[$today];
- $yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;
- echo "document.write('总访问 {$total}, 本月 {$month}, 昨日 {$yesterday}, 今日 {$today}');";
使用方法:将以上代码添加至tongji.php里面,然后给要统计的页面加上下面js代码
调用方法:
作者:admin
链接:https://www.wzlingdi.com/cms/detail/index?id=72594
来源:网站领地导航
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
上一篇:欢迎使用呆错文章管理系统
下一篇:抖音官网百科