第一种方法:基于文件系统的页面计数器
<?php
$file = "num.txt";
$fp = fopen($file,"r+");
if($fp == false){
echo "打开失败";
exit;
}
else{
$count = fread($fp,20);
$count = $count+1;
fclose($fp);
$fp = fopen($file,"w+");
fwrite($fp,$count);
fclose($fp);
echo "您是第【".$count."】位访客";
}
?>
第二种方法:基于数据库的页面计数器
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("message");
$query = "select * from num";
$result = mysql_query($query);
if($result)
{
$r = mysql_fetch_array($result);
$count1 = $r["count"];
$count2 = $count1+1;
$update = "update num set count= $count2 where id=1";
$result = mysql_query($update);
mysql_close();
echo "您是第:".$count2."位访客";
}
?>
第三种方法:页面分离计数器
首先建立index.php文件:
<?php
$file = "num.txt";
$fp = fopen($file,"r+");
if($fp == false){
echo " 打开文件失败";
}
else{
$count1 = fread($fp,20);
$count2 = $count1+1;
echo " document.write(\" $count2 \")\n";
$fp = fopen($file,"w+");
fwrite($fp,$count2);
fclose($fp);
}
?>
再建立index.html文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
</head>
<body>
您是第:
<script language="JavaScript" src="index3.php">
</script>
为访客
</body>
</html>
第四种方法:图像样式计数器
<?php
$file = "num.txt";
if (($fp = fopen($file , "r+")) == false) { //用读写模式打开文件,若不能打开就退出
printf ("打开文件 %s 失败!",$file);
exit;
}
else
{
$count = fread ($fp,10);
$count = $count + 1;
fclose ($fp);
$fp = fopen($file , "w+");
fwrite ($fp,$count);
fclose ($fp);
}
$fp = fopen($file, "r"); //以只读模式打开文件
$array_count = 1; //定义一个表示数组元素位置的变量,下面要用
while (!feof($fp)) {
$current_number = fgetc($fp);
$counter_array[$array_count] = $current_number;
$array_elements = count ($counter_array);
$array_count = $array_count + 1;
}
//显示代码
echo ("<table border=\"0\" height=\"5\" align=\"left\"><tr><td align=\"center\">欢迎您,第");
for ($array_id = 1;$array_id < $array_elements; ++ $array_id) {
echo ("<img src=\"./$counter_array[$array_id].gif\" align=absmiddle>");
}
echo ("位客人</td></tr></table>");
?>
第五种方法:生成计数图片计数器:
demo.php
<HTML>
<HEAD>
<TITLE>图形计数器范例</TITLE>
</HEAD>
<BODY>
您好,您是第<img src="ImgOutFileCount.php">位访客
</BODY>
</HTML>
ImgOutFileCount.php
<?
Header("Content-type: image/gif");
//http头,告诉浏览器,这是一个GIF图片
$countfile = "num.txt";
//定义计数器写入的文件是当前目录下count.txt,然后我们应当测试该文件能否打开
if (($fp = fopen($countfile, "r+")) == false) { //用读写模式打开文件,若不能打开就退出
printf ("打开文件 %s 失败!",$countfile);
exit;
}
else
{
//如果文件能够正常打开,就读入文件中的数据,假设是1
$count = fread ($fp,10);
//读取10位数据
$count = $count + 1;
fclose ($fp);
//关闭当前文件
$fp = fopen($countfile, "w+");
//以覆盖模式打开文件
fwrite ($fp,$count);
//写入加1后的新数据
fclose ($fp);
//并关闭文件
}
//定义输出为图像类型
$n=10;
//变量$n是显示位数
//利用上面的方法,取得访问人数并赋值给变量$str1 (程序略)
$str1=$count;
$str2 = "";
//位数如果不够$n位,在前面补0
$len1 = strlen($str1);
for ($i=1;$i<=$n;$i++) {
$str2 = "0".$str2;
};
//得到$n位0
$len2 = strlen($str2);
//计算访问人数的位数
$dif = $len2 - $len1;
$rest = substr($str2, 0, $dif);
$string = $rest.$str1;
//位数如果不够$n位,在前面补0
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);
//释放图象
?>
第六种:防刷新计数器
<?php
session_start();
$allow_sep = "60";
$file = "num.txt";
if (($fp = fopen($file , "r+")) == false) { //用读写模式打开文件,若不能打开就退出
printf ("打开文件 %s 失败!",$file);
exit;
}
else
{
if(isset($_SESSION["post_sep"])){
if(time() - $_SESSION["post_sep"] < $allow_sep){
exit("请不要反复刷新");
}
else{
$_SESSION["post_sep"]=time();
}
}
else{
$_SESSION["post_sep"]=time();
}
if($_SESSION["post_sep"]=time()){
$file = "num.txt";
$count = fread ($fp,10);
$count = $count + 1;
fclose ($fp);
$fp = fopen($file , "w+");
fwrite ($fp,$count);
fclose ($fp);
$fp = fopen($file, "r"); //以只读模式打开文件
$array_count = 1; //定义一个表示数组元素位置的变量,下面要用
while (!feof($fp)) {
$current_number = fgetc($fp);
$counter_array[$array_count] = $current_number;
$array_elements = count ($counter_array);
$array_count = $array_count + 1;
}
//显示代码
echo ("<table border=\"0\" height=\"5\" align=\"left\"><tr><td align=\"center\">欢迎您,第");
for ($array_id = 1;$array_id < $array_elements; ++ $array_id) {
echo ("<img src=\"./$counter_array[$array_id].gif\" align=absmiddle>");
}
echo ("位客人</td></tr></table>");
} }