当前位置:首页 > PHP源码 > 列表

PHP简单实现记录网站访问量功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2018-11-07 12:31:38 浏览: 评论:0 

本文实例讲述了PHP简单实现记录网站访问量功能。分享给大家供大家参考,具体如下:

tongji/index.php文件:

  1. $file = dirname(__FILE__).'/tongji.db'; 
  2. //$data = unserialize(file_get_contents($file)); 
  3. $fp=fopen($file,'r+'); 
  4. $content=''; 
  5. if (flock($fp,LOCK_EX)){ 
  6. while (($buffer=fgets($fp,1024))!=false){ 
  7. $content=$content.$buffer; 
  8. } 
  9. $data=unserialize($content); 
  10. //设置记录键值 
  11. $total = 'total'; 
  12. $month = date('Ym'); 
  13. $today = date('Ymd'); 
  14. $yesterday = date('Ymd',strtotime("-1 day")); 
  15. $tongji = array(); 
  16. // 总访问增加 
  17. $tongji[$total] = $data[$total] + 1; 
  18. // 本月访问量增加 
  19. $tongji[$month] = $data[$month] + 1; 
  20. // 今日访问增加 
  21. $tongji[$today] = $data[$today] + 1; 
  22. //保持昨天访问 
  23. $tongji[$yesterday] = $data[$yesterday]; 
  24. //保存统计数据 
  25. ftruncate($fp,0); // 将文件截断到给定的长度 
  26. rewind($fp); // 倒回文件指针的位置 
  27. fwrite($fp, serialize($tongji)); 
  28. flock($fp,LOCK_UN); 
  29. fclose($fp); //phpfensi.com 
  30. //输出数据 
  31. $total = $tongji[$total]; 
  32. $month = $tongji[$month]; 
  33. $today = $tongji[$today]; 
  34. $yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0; 
  35. echo "document.write('访总问 {$total}, 本月 {$month}, 昨日 {$yesterday}, 今日 {$today}');"; 
  36. } 

使用方法(通过js引入tongji/index.php文件):

  1. <script language="JavaScript" src="./tongji/"></script> 

Tags: PHP计数器 PHP访问量

分享到:

相关文章