首先建立:function.php
<?php
function counter(){
$file='counter.txt';
if(!file_exists($file)){
$num = 0;
$cf = fopen($file,"w");
fwrite($cf,$num);
fclose($cf);
}else{
$cf = fopen($file,"rw");
$num=fgets($cf);
fclose($cf);
}
$num++;
$cf=fopen($file,"w");
fwrite($cf,$num);
fclose($cf);
return $num;
}
?>
再建立一个访问网页:counter.php
<?php
require("function.php");
?>
<html>
<head>
<title>访客计数器</title>
</head>
<body>
<h1>欢迎访问</h1>
<br />
<hr />
<br />
您是第<?php echo counter();?>访客
</body>
</html>
运行counter.php后,你会发现在项目目录下建立了一个counter.txt的文件,同时实现网页 的简易统计。当然没有实现防止刷新技术和同一个ip提交的问题。