PHP计数器(文本与图片二种)的代码

代码1,文本文件计数器。
<?php
/*文本文件记录数据 计数器*/
$counter=1;
if(file_exists("mycounter.txt")){
$fp=fopen("mycounter.txt","r");
$counter=fgets($fp,9);
$counter++;
fclose($fp);
}
$fp=fopen("mycounter.txt","w");
fputs($fp,$counter);
fclose($fp);
echo "<h1>您是第".$counter."次访问本页面!<h1>";
//by www.jbxue.com
?>
代码2,简单数据库计数器:
<?php
//基于数据库的简单计数器,未添加防刷新功能哦
$conn=mysql_connect("localhost","root","abc");
$result=mysql_query("use db_counter");
$re=mysql_query("select * from tb_counter");
$result=mysql_fetch_row($re);
$counter=$result[0];
echo "您是第{$counter}位访问者!";
$counter+=1;echo "<hr>{$counter}";
mysql_query("update tb_counter set counter=$counter");
mysql_close($conn);
//by www.jbxue.com
?>

你可能感兴趣的:(计数器)