php计数器的简单代码举例
时间:2021-07-01 10:21:17
帮助过:31人阅读
/* - * 例1,文本文件记录数据
- * edit bbs.it-home.org
- */
- $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 "
您是第".$counter."次访问本页面!"; //例2,基于数据库的简单计数器 - $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 "
{$counter}"; - mysql_query("update tb_counter set counter=$counter");
- mysql_close($conn);
- //只是一个简单的计数器,大家可以加入防止重复刷新的功能
- ?>
|