php简单自主开网页访问次数计数器代码
这里是我们用php简单自主开网页访问次数计数器代码偌,是一款基于文件的图形计数器范例代码,会用到的文件用:num.txt //累计访问次数,vist.php//测试文件,count.php//统计访问次的核心程序,因为它会把文字转换成gif图片,并且输出到浏览器.
PHP访问次数计数器html代码如下:
- <HTML>
- <HEAD>
- <TITLE>图形计数器范例</TITLE>
- </HEAD>
- <BODY>
- 您好,您是第<img src="ImgOutFileCount.php">位访客
- </BODY>
- </HTML>
count.php文件,代码如下:
- <?php
- Header("Content-type: image/gif");
-
- $countfile = "num.txt";
-
- if (($fp = fopen($countfile, "r+")) == false) {
- printf ("打开文件 %s 失败!",$countfile);
- exit;
- }
- else
- {
-
- $count = fread ($fp,10);
-
- $count = $count + 1;
- fclose ($fp);
-
- $fp = fopen($countfile, "w+");
-
- fwrite ($fp,$count);
-
- fclose ($fp);
-
- }
-
- $n=10;
-
-
-
- $str1=$count;
- $str2 = "";
-
- $len1 = strlen($str1);
- for ($i=1;$i<=$n;$i++) {
- $str2 = "0".$str2;
- };
-
- $len2 = strlen($str2);
-
- $dif = $len2 - $len1;
- $rest = substr($str2, 0, $dif);
- $string = $rest.$str1;
-
- for ($i=0;$i<=$n-1;$i++) {
- $str[$i]=substr($string,$i,1);
- };
-
- $font = 4;
-
- $im = imagecreate($n*11-1,16);
-
- $black = ImageColorAllocate($im, 0,0,0);
- $white = ImageColorAllocate($im, 255,255,255);
-
- imagefill($im, 0,0,$black);
-
- ImageString($im,$font,1,0,$str[0],$white);
- for ($i=1;$i<=$n-1;$i++) {
- imageline($im, $i*11-1,0,$i*11-1,16, $white);
- ImageString($im,$font,$i*11+1,0,$str[$i],$white);
- };
-
- ImageGif($im);
-
- ImageDestroy($im);
-
- ?>