count.php
<?php
require_once 'countAction.php';
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<div>
<span>访问者计数器</span>
<div>
<span>你是第个<span><?php Counter();?></span>访问者</span>
</div>
</div>
</body>
</html>
countAction.php
<?php
function Counter()
{
$five = "00000";
$four = "0000";
$three = "000";
$two = "00";
$one = "0";
$counter = "record.dat";
if(!file_exists($counter))
{
$counter = fopen($counter,"w");
fputs($counter,"0");
fclose($counter);
}else{
$fp = fopen($counter,"r+");
$num = fgets($fp,"1024");
$num = $num + 1;
if($num < 10)
print "$five"."$num";
elseif($num < 100)
print "$four"."$num";
elseif($num < 1000)
print "$three"."$num";
elseif($num < 10000)
print "$two"."$num";
elseif($num < 100000)
print "$one"."$num";
else
print "$num";
}
$fp = fopen($counter,"w");
fputs($fp,"$num");
fclose($fp);
}