为thinkphp网站添加昨日今日总访问统计代码

2024-10-11 12:29:05 建站经验 thinkphp网站添加昨日今日总访问统计代码

为thinkphp网站添加昨日今日总访问统计代码

实现 总访问 , 本月访问, 昨日访问, 今日访问统计,使用文本存储,简单好用 自动创建文本数据库,保存本php同目录。显示效果如图:

屏幕截图_11-10-2024_122518_www.wzlingdi.com.jpeg

  1. error_reporting(0);
  2. header ( "Content-type: text/html; charset=utf-8" );
  3. //设置数据文件路径
  4. $file = dirname(__FILE__).'/tongji.db';
  5. $data = unserialize(file_get_contents($file));
  6. //设置记录键值
  7. $total = 'total';
  8. $month = date('Ym');
  9. $today = date('Ymd');
  10. $yesterday = date('Ymd',strtotime("-1 day"));
  11. $tongji = array();
  12. // 总访问增加
  13. $tongji[$total] = $data[$total] + 1;
  14. // 本月访问量增加
  15. $tongji[$month] = $data[$month] + 1;
  16. // 今日访问增加
  17. $tongji[$today] = $data[$today]  + 1;
  18. //保持昨天访问
  19. $tongji[$yesterday] = $data[$yesterday];
  20. //保存统计数据
  21. file_put_contents($file, serialize($tongji));
  22. //输出数据
  23. $total = $tongji[$total];
  24. $month = $tongji[$month];
  25. $today = $tongji[$today];
  26. $yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;
  27. echo  "document.write('总访问 {$total}, 本月 {$month}, 昨日 {$yesterday}, 今日 {$today}');";

使用方法:将以上代码添加至tongji.php里面,然后给要统计的页面加上下面js代码

调用方法:

1

作者:admin

链接:https://www.wzlingdi.com/cms/detail/index?id=72594

来源:网站领地导航

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

上一篇:欢迎使用呆错文章管理系统

下一篇:抖音官网百科