今天写的是上一篇的进阶版。用图片输出访问量数字。
首先,我们建立一个文件夹命名为“img-Count”
在根目录“img-Count”下,建立一个img文件夹用来存储数字对应的图片。
把每张图片用“jsq*.gif”命名。
创建php文件”count_p.inc.php”,键入代码如下:
<?php
header("Content_type:img/gif");
function counter(){
$counter = 0;
$lj = explode("/", $_SERVER['PHP_SELF']);
$CounterFile = "./counter/".$lj[count ($lj)-1].".dat";
if(!file_exists($CounterFile)){
if(!file_exists(dirname($CounterFile))){
mkdir(dirname($CounterFile),0777);
}
$cf = fopen($CounterFile, 'w');
fputs($cf,'0');
fclose($cf);
}
else{
$cf = fopen($CounterFile,'r');
$counter = trim(fgets($cf));
fclose($cf);
}
$counter++;
$cf = fopen($CounterFile,'w');
fputs($cf,$counter);
fclose($cf);
$temp = (string)$counter;
$size = strlen($temp);
for($i=0;$i<$size;$i++){
$p=substr($temp,$i,1);
echo ("<img src='"."./img/sjq".($p).".gif' vspace='10' align='middle'>");
}
}
?>
创建php文件“al_ImgCount.php”,键入如下代码:
<?php
include "counter_p.inc.php";
?>
<html>
<head>
<meta charset="UTF-8">
<title>图片式网站访问量</title>
</head>
<body>
<?php
counter();
?>
</body>
</html>
其实就只要一个输出环境罢了。
好了,现在让我来解释一下上面输出图片的代码吧!
$temp = (string)$counter 把$counter转换成字符串类型并赋给$temp
$size = strln($temp); 把字符串的长度赋给$size
for循环 循环的是每个数字
$p = substr($temp,$i,1); 从$temp里的$i位置开始截取1个单位字符串 并赋值给$p
header('Content_type:img/gif') 告诉浏览器,这个网页含有gif图像文件。这行语句应该放在html标签之前。
好了,图片我就不上传了。
数字图片在网上一搜一大把。
感谢阅读。