前言

为了给我的成绩查询网站增加一个使用者统计功能,所以我就想到了给网站前端加一个代码,使其每刷新一次,数字加1


代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 $filename = './count.txt'; //第一步:创建文件并初始化为0;

if(!file_exists($filename)){

//打开方式"w",文件不存在,则创建之

$handle = fopen($filename, 'w');

fwrite($handle, 0);//将0写入count.txt

fclose($handle);//关闭文件释放资源

}

//第二步: 打开文件,读取内容,并加1后,再写入count.txt

$handle = fopen($filename, 'r+');

//将count.txt中数字读出来

$line = fgets($handle);

$line++;

//变量自加1

//再将变量line写入count.txt

rewind($handle);

//将指针复位;

fwrite($handle, $line);

fclose($handle);

//关闭文件释放资源

前端代码

1
<p style="font-size:20px">本站已提供<span style="color: #769164"><?php echo $line; ?></span>次服务</p> 

注意

第一部分代码只能在php下执行,不能在html下执行,第二个代码是html代码,所以在php文件中第二段代码放在html标签下,即可。


效果

001