【php开源代码栏目提醒】:以下是网学会员为您推荐的php开源代码-EX15-03.php,希望本篇文章对您学习有所帮助。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<meta http-equiv="Content-Language" content="zh-tw">
<title>文字版的访问者计数器</title>
</head>
<body background="images/BG01.jpg">
<table width=30% align=center><tr><td>
<fieldset>
<legend >访问者计数器(文字版)</legend>
<center> 目前的访问者人数:<?echo GDImage(counter());?><center>
</fieldset>
</td></tr></table>
</body>
</html>
<?
function counter(){
$file="cdetail.txt";
if(@file_exists($file)){
chmod($file,0222);
if(@$open_file=fopen($file,"r+")){
$text=fread($open_file,filesize($file));
++$text;
$open_file=fopen($file,"r+");
fwrite($open_file,$text,strlen($text));
fclose($open_file);
chmod($file,0444);
return $text;
exit;
}else{
return "打开计数文件失败!";
exit;
}
}else{
$open_file=fopen($file,"w");
if(@fwrite($open_file,"1",1)==false){
return "写入文件失败!";
exit;
}else
{
return "1";
}
}
}
function GDImage($text){
$imagefile="images/gdcounter.gif";
$counterlen=strlen($text);
$charHeight=ImageFontHeight(2);
$charWidth=ImageFontWidth(2);
$strHeight=$charHeight;
$strWidth=$charWidth*$counterlen;
$imgCx=$strWidth/2;
$imgCy=$strHeight/2;
$red=ImageColorAllocate($im,255,255,255);
$black=ImageColorAllocate($im,0,0,0);
$im=ImageCreate($strWidth,$strHeight);
ImageFilledRectangle($im,0,0,$strWidth,$strHeight,$red);
$drawPosX=$imgCx-($strWidth/2)+1;
$drawPoxY=$imgCy-($strHeight/2);
ImageString($im,5,$drawPosX,$drawPoxY,$text,$black);
ImageGif($im,$imagefile);
return $imagefile;
}
?>