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ù)載正常。
<?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ā)的吧。
復(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)文章
php echo, print, print_r, sprintf, var_dump, var_expor的使用區(qū)別
本篇文章是對(duì)php中echo, print, print_r, sprintf, var_dump, var_expor的使用區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php實(shí)現(xiàn)用于計(jì)算執(zhí)行時(shí)間的類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)用于計(jì)算執(zhí)行時(shí)間的類,實(shí)例分析了php計(jì)算運(yùn)行實(shí)現(xiàn)的類實(shí)例與相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
PHP實(shí)現(xiàn)異步延遲消息隊(duì)列的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用PHP+Laravel+RabbitMQ來實(shí)現(xiàn)異步延遲消息隊(duì)列,文中的實(shí)現(xiàn)過程講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-05-05
PHP轉(zhuǎn)Go之?dāng)?shù)組的正確使用詳解
這篇文章主要為大家對(duì)比一下PHP中的Array和Golang中的 Array&Slice&Map,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
php使用cookie顯示用戶上次訪問網(wǎng)站日期的方法
這篇文章主要介紹了php使用cookie顯示用戶上次訪問網(wǎng)站日期的方法,主要通過使用cookie保存訪問時(shí)間來實(shí)現(xiàn)該功能,需要的朋友可以參考下2015-01-01

