您好,欢迎访问一九零五行业门户网

PHP禁止同一IP频繁访问以防止网站被防攻击或采集的代码

php禁止同一ip频繁访问以防止网站被防攻击或采集的代码
30){
@unlink($fileforbid);
}else{
$fileforbidarr=@file($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.\r\n,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;
$ipdate=@file($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.\r\n;
}else if($numtem<$allownum){
$str.=$iptem.$uritem.$timetem.($numtem+1).\r\n;
}
else{
if(!file_exists($fileforbid)){
$addforbidarr=array($ip.\r\n,time().\r\n,1);
file_put_contents($fileforbid,$addforbidarr);
}
file_put_contents($logfilepath.'forbided_ip.log',$ip.'--'.date('y-m-d h:i:s',time()).'--'.$uri.\r\n,file_append);
$timepass=$timetem+$allowtime-$time;
exit('警告:不要刷新的太频繁!');
}
}
}
}
if($yesno){
$str.=$checkip.$checkuri.$time.\r\n;
}
file_put_contents($file,$str);
其它类似信息

推荐信息