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

WBOY
Release: 2016-06-20 13:01:54
Original
1852 people have browsed it

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

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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!