当前位置:Gxlcms > PHP教程 > php页面添加计数器

php页面添加计数器

时间:2021-07-01 10:21:17 帮助过:5人阅读

index.php

1php 2 include("counter.php") 3?> 4 5<html> 6<head> 7<title>访问计数器title> 8head> 910<body>11<center>12<h1>访问者计数器h1>13<U>14<font size="6" face="隶书">你是第font>15<B>16<font size="9" face="Verdana" color="#ffcc00">17php Counter() ?>18font>19B>20<font size="6" face="隶书">个访问者font>21U>22center>23body>24html>

counter.php

1php 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?>

以上就介绍了php页面添加计数器,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行