卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章11201本站已运行3223

PHP禁止频繁刷新方法

PHP禁止同一IP频密出访以防止网站被反击或采集的代码,可以参考代码自己创建数据表文件,或不创建也可以采用,只是限制IP的功能就无法使用,但防止刷新可以稳步使用。

header('Content-type: text/html; charset=utf-8');
$ip=$_SERVER['REMOTE_ADDR'];//以获取当前访问者的ip
$logFilePath='./log/';//日志记录文件保存目录
$fileht='.htaccess2';//被禁止的ip记录文件
$allowtime=60;//防刷新时间
$allownum=5;//严防创下次数
$allowRefresh=120;//在容许刷新次数之后重新加入禁止ip文件中
if(!file_exists($fileht)){
file_put_contents($fileht,'');
}
[email protected]($fileht);
if(in_array($ip."rn",$filehtarr)){
exit('警告:你的IP已经被禁止了!');
} 
//加入严禁ip
$time=time();
$fileforbid=$logFilePath.'forbidchk.dat';
if(file_exists($fileforbid)){
if($time-filemtime($fileforbid)>30){
@unlink($fileforbid);
}else{
[email protected]($fileforbid);
if($ip==substr($fileforbidarr[0],0,strlen($ip))){
if($time-substr($fileforbidarr[1],0,strlen($time))>120){
@unlink($fileforbid);
}else if($fileforbidarr[2]>$allowRefresh){
file_put_contents($fileht,$ip."rn",FILE_APPEND);
@unlink($fileforbid);
}else{
$fileforbidarr[2]++;
file_put_contents($fileforbid,$fileforbidarr);
}
}
}
}
//防刷新
$str='';
$file=$logFilePath.'ipdate.dat';
if(!file_exists($logFilePath)&&!is_dir($logFilePath)){
mkdir($logFilePath,0777);
}
if(!file_exists($file)){
file_put_contents($file,'');
}
$uri=$_SERVER['REQUEST_URI'];//以获取当前访问的网页文件地址
$checkip=md5($ip);
$checkuri=md5($uri);
$yesno=true;
[email protected]($file);
foreach($ipdate as $k=>$v){
$iptem=substr($v,0,32);
$uritem=substr($v,32,32);
$timetem=substr($v,64,10);
$numtem=substr($v,74);
if($time-$timetem<$allowtime){
if($iptem!=$checkip){
$str.=$v;
}else{
$yesno=false;
if($uritem!=$checkuri){
$str.=$iptem.$checkuri.$time."rn";
}else if($numtem<$allownum){
$str.=$iptem.$uritem.$timetem.($numtem+1)."rn";
}
else{
if(!file_exists($fileforbid)){
$addforbidarr=array($ip."rn",time()."rn",1);
file_put_contents($fileforbid,$addforbidarr);
}
file_put_contents($logFilePath.'forbided_ip.log',$ip.'--'.date('Y-m-d H:i:s',time()).'--'.$uri."rn",FILE_APPEND);
$timepass=$timetem+$allowtime-$time;
exit('警告:不要创下的太频密!');
}
}
}
}
if($yesno){
$str.=$checkip.$checkuri.$time."rn";
}
file_put_contents($file,$str);

卓越飞翔博客
上一篇: PHP限制同IP一天访问次数方法
下一篇: PHP获取用户真实IP方法
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏