当前位置:首页 > PHP教程 > php应用 > 列表

php使用文本统计访问量的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-08-03 15:25:30 浏览: 评论:0 

这篇文章主要介绍了php使用文本统计访问量的方法,涉及php文本文件读写与数值运算的相关技巧,需要的朋友可以参考下,本文实例讲述了php使用文本统计访问量的方法,分享给大家供大家参考,具体如下:

方法1:

  1. $fp = fopen("counter.txt""r+"); 
  2. while(!flock($fp, LOCK_EX)) { // acquire an exclusive lock 
  3.   // waiting to lock the file 
  4. $counter = intval(fread($fpfilesize("counter.txt"))); 
  5. $counter++; 
  6. ftruncate($fp, 0);   // truncate file 
  7. fwrite($fp$counter); // set your data 
  8. fflush($fp);      // flush output before releasing the lock 
  9. flock($fp, LOCK_UN);  // release the lock 
  10. fclose($fp); 

方法2:

counter.php文件:

  1. <?php 
  2. /* counter */ 
  3. //opens countlog.txt to read the number of hits 
  4. $datei = fopen("countlog.txt","r"); 
  5. $count = fgets($datei,1000); 
  6. fclose($datei); 
  7. $count=$count + 1 ; 
  8. echo "$count" ; 
  9. echo " hits" ; 
  10. echo "\n" ; 
  11. // opens countlog.txt to change new hit number 
  12. $datei = fopen("countlog.txt","w"); 
  13. fwrite($datei$count); 
  14. fclose($datei); 
  15. ?> 

用法:

  1. <?php 
  2. include("counter.php"); 
  3. ?>

Tags: php文本统计访问量

分享到: