index.php
1 <?php 2 include("counter.php") 3 ?> 4 5 <html> 6 <head> 7 <title>访问计数器</title> 8 </head> 9 10 <body> 11 <center> 12 <h1>访问者计数器</h1> 13 <U> <!--下划线--> 14 <font size="6" face="隶书" >你是第</font> 15 <B> 16 <font size="9" face="Verdana" color="#ffcc00"> 17 <?php Counter() ?> <!--调用函数--> 18 </font> 19 </B> 20 <font size="6" face="隶书" >个访问者</font> 21 </U> 22 </center> 23 </body> 24 </html>
counter.php
1 <?php 2 //$five,$four 等变量表示零的个数,放在数字前边构成6位 3 function Counter() 4 { //定义函数 5 $five = "00000"; //声明变量 6 $four = "0000"; 7 $three = "000"; 8 $two = "00"; 9 $one = "0"; 10 $counter = "record.dat"; //存放访问数的目的文件,.dat格式 11 if(!file_exists($counter)) //判断文件是否存在 12 { 13 $counter = fopen($counter,"w"); 14 fputs($counter,"0"); //写入文件 15 fclose($counter); 16 } 17 else 18 { 19 $fp = fopen($counter,"r+"); 20 $num = fgets($fp,"1024"); //如果文件存在则读出文件,并加 1 21 $num = $num + 1; 22 if($num < 10) 23 print "$five"."$num"; 24 elseif($num < 100) 25 print "$four"."$num"; 26 elseif($num < 1000) 27 print "$three"."$num"; 28 elseif($num < 10000) 29 print "$two"."$num"; 30 elseif($num < 100000) 31 print "$one"."$num"; 32 else 33 print "$num"; 34 } 35 $fp = fopen($counter,"w"); 36 fputs($fp,"$num"); 37 fclose($fp); 38 } 39 ?>