php使用文本统计访问量的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-08-03 15:25:30 浏览: 评论:0
这篇文章主要介绍了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");
- ?>
Tags: php文本统计访问量

推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)