最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php下網(wǎng)站防IP攻擊代碼,超級(jí)實(shí)用

 更新時(shí)間:2010年10月24日 20:38:59   作者:  
現(xiàn)在做外國網(wǎng)絡(luò),訪問量越來越高了,最近有很多不良IP不停的進(jìn)行攻擊,由于不是自己的主機(jī),所以,只能通過代碼去阻止它們。
今天我開發(fā)了下面的代碼,算是大功初成,一天攔截了15個(gè)IP,服務(wù)器負(fù)載正常。
復(fù)制代碼 代碼如下:

<?php
//查詢禁止IP
$ip =$_SERVER['REMOTE_ADDR'];
$fileht=".htaccess2";
if(!file_exists($fileht))file_put_contents($fileht,"");
$filehtarr=@file($fileht);
if(in_array($ip."\r\n",$filehtarr))die("Warning:"."<br>"."Your IP address are forbided by some reason, IF you have any question Pls emill to shop@mydalle.com!");

//加入禁止IP
$time=time();
$fileforbid="log/forbidchk.dat";
if(file_exists($fileforbid))
{ if($time-filemtime($fileforbid)>60)unlink($fileforbid);
else{
$fileforbidarr=@file($fileforbid);
if($ip==substr($fileforbidarr[0],0,strlen($ip)))
{
if($time-substr($fileforbidarr[1],0,strlen($time))>600)unlink($fileforbid);
elseif($fileforbidarr[2]>600){file_put_contents($fileht,$ip."\r\n",FILE_APPEND);unlink($fileforbid);}
else{$fileforbidarr[2]++;file_put_contents($fileforbid,$fileforbidarr);}
}
}
}
//防刷新
$str="";
$file="log/ipdate.dat";
if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);
if(!file_exists($file))file_put_contents($file,"");
$allowTime = 120;//防刷新時(shí)間
$allowNum=10;//防刷新次數(shù)
$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."1\r\n";
elseif($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("log/forbided_ip.log",$ip."--".date("Y-m-d H:i:s",time())."--".$uri."\r\n",FILE_APPEND);
$timepass=$timetem+$allowTime-$time;
die("Warning:"."<br>"."Sorry,you are forbided by refreshing frequently too much, Pls wait for ".$timepass." seconds to continue!");
}
}
}
}
if($yesno) $str.=$checkip.$checkuri.$time."1\r\n";
file_put_contents($file,$str);
?>

把程序include進(jìn)要進(jìn)行的php文件頭部。
程序首先判斷IP是否在禁止列表,如果在則退出;
否則,如果IP在監(jiān)控列表,10分鐘內(nèi)點(diǎn)擊超過600次則加入禁止列表。
如果沒有超過時(shí)間和次數(shù)則次數(shù)加1,同時(shí),監(jiān)控IP是否對(duì)同一頁面頻繁操作。
由于是機(jī)器刷頻,所以程序不用session判斷。
還有就是更新的時(shí)候,不能用獨(dú)占處理文件。
使用偽靜態(tài)存儲(chǔ)IP也會(huì)出錯(cuò)。
這個(gè)程序還有變化和改進(jìn)的余地的,大家也可以參考QQ農(nóng)場(chǎng)里的降級(jí)原理,基本上農(nóng)場(chǎng)也是按照這個(gè)原理開發(fā)的吧。

相關(guān)文章

最新評(píng)論

九台市| 永善县| 阳泉市| 扎兰屯市| 枞阳县| 柘荣县| 新乐市| 福州市| 靖边县| 象州县| 莎车县| 土默特左旗| 定南县| 西城区| 修水县| 和平区| 丽江市| 阳山县| 临邑县| 安吉县| 阜康市| 简阳市| 松原市| 公安县| 吐鲁番市| 紫阳县| 青冈县| 西充县| 滦南县| 延寿县| 沅江市| 西乡县| 中宁县| 滦南县| 西乌| 田阳县| 天峨县| 什邡市| 玛纳斯县| 高清| 新疆|