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

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

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

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

相關文章

最新評論

洛阳市| 米泉市| 黔西县| 宝应县| 沙坪坝区| 石嘴山市| 洪泽县| 平邑县| 刚察县| 商洛市| 遂平县| 黑水县| 东乌| 五家渠市| 双江| 平阴县| 兴海县| 西贡区| 易门县| 盈江县| 青阳县| 巴林右旗| 荣成市| 子洲县| 镇坪县| 大荔县| 古丈县| 自治县| 青川县| 武宣县| 上蔡县| 齐河县| 抚顺县| 孝昌县| 佳木斯市| 邵武市| 历史| 维西| 龙南县| 嘉善县| 巴青县|