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

yipeiwu_com6年前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文件操作总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP6 先修班 JSON实例代码

它是基於JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個子集 JSON...

PHP Header用于页面跳转要注意的几个问题总结

1.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法。header()函数的主要功能是将HTTP协议标头(header)输出到浏览器。 header(...

PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能示例

本文实例讲述了PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能。分享给大家供大家参考,具体如下: 这里模拟tp框架模板变量分配与赋值操作。 ext...

PHP中opcode缓存简单用法分析

本文实例讲述了PHP中opcode缓存简单用法。分享给大家供大家参考,具体如下: 1.什么是opcode 解释器分析代码之后,生成可以直接运行的中间代码,就称做操作码,opcode 2....

php采用session实现防止页面重复刷新

如何防止页面重复刷新,在php环境下可以利用session来轻松实现。 b.php的代码 <?php //只能通过post方式访问 if ($_SERVER['...