首页  >  文章  >  后端开发  >  php使用文本统计访问量的方法实例详解

php使用文本统计访问量的方法实例详解

怪我咯
怪我咯原创
2017-07-04 13:17:511077浏览

这篇文章主要介绍了php使用文本统计访问量的方法,涉及php文本文件读写与数值运算的相关技巧,需要的朋友可以参考下

本文实例讲述了php使用文本统计访问量的方法。分享给大家供大家参考,具体如下:

方法1:

$fp = fopen("counter.txt", "r+");
while(!flock($fp, LOCK_EX)) { // acquire an exclusive lock
  // waiting to lock the file
}
$counter = intval(fread($fp, filesize("counter.txt")));
$counter++;
ftruncate($fp, 0);   // truncate file
fwrite($fp, $counter); // set your data
fflush($fp);      // flush output before releasing the lock
flock($fp, LOCK_UN);  // release the lock
fclose($fp);

方法2:

counter.php文件:

<?php
/* counter */
//opens countlog.txt to read the number of hits
$datei = fopen("countlog.txt","r");
$count = fgets($datei,1000);
fclose($datei);
$count=$count + 1 ;
echo "$count" ;
echo " hits" ;
echo "\n" ;
// opens countlog.txt to change new hit number
$datei = fopen("countlog.txt","w");
fwrite($datei, $count);
fclose($datei);
?>

用法:

<?php
include("counter.php");
?>

以上是php使用文本统计访问量的方法实例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn