防止一天内重复刷新计数器

<?php
error_reporting(E_ALL ^ E_NOTICE);
$counter_file="counter.txt"; //文件名赋值给变量
if(!file_exists($counter_file)){
$myfile=fopen($counter_file,'w'); //创建文件
fwrite($myfile,"1"); //值入0
fclose($myfile); //关闭文件
}
$t_num=file($counter_file);
if($_COOKIE["date"]!="date(Y年m月d日)"){ //判断COOKIE内容与当前日期是否一致
$t_num[0]++; //原始数据自增1
$myfile=fopen($counter_file,"w"); //写入方式打开文件
fwrite($myfile,$t_num[0]); //写入新数值
fclose($myfile); //关闭文件
setcookie("date","date(Y年m月d日)",time()+60*60*24);//重新将当前日期写入cookie的有效期为24小时
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charest=utf-8"/>
<title>防止一天内重复刷新计数器</title>
</head>
<body>
<?php
echo "欢迎!您是本站第"; //显示头部内容
$myfile=fopen($counter_file,"r"); //以只读的方式打开文件
while(!feof($myfile)){ //循环读出文件内容
$num=fgetc($myfile); //当前指针处字符赋值给变量
if($num==""){ //判断字符是否为空字符
break; //遇到空字符表示到了最后一个字符,退出循环
}else{ //如果数值存在则执行操作
echo $num; //显示相应的数字
}
}
fclose($myfile); //关闭文件
echo "位访客!";
?>
</body>
</html>

原文地址:https://www.cnblogs.com/yxhblogs/p/4376181.html