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)場里的降級(jí)原理,基本上農(nó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)場里的降級(jí)原理,基本上農(nóng)場也是按照這個(gè)原理開發(fā)的吧。
相關(guān)文章
php設(shè)計(jì)模式之適配器模式原理、用法及注意事項(xiàng)詳解
這篇文章主要介紹了php設(shè)計(jì)模式之適配器模式原理、用法及注意事項(xiàng),結(jié)合實(shí)例形式分析了php適配器模式的定義、用法及出現(xiàn)的問題與解決方法,需要的朋友可以參考下2019-09-09如何使用SublimeText3配置 PHP IDE環(huán)境
這篇文章主要介紹了如何使用SublimeText3配置 PHP IDE環(huán)境,并使用Xdebug進(jìn)行調(diào)試,喜歡使用SublimeText的同學(xué),可以參考下2021-04-04WordPress開發(fā)中的get_post_custom()函數(shù)使用解析
這篇文章主要介紹了WordPress開發(fā)中的get_post_custom()函數(shù)使用解析,get_post_custom()函數(shù)用于獲取自定義字段,需要的朋友可以參考下2016-01-01golang與php實(shí)現(xiàn)計(jì)算兩個(gè)經(jīng)緯度之間距離的方法
這篇文章主要介紹了golang與php實(shí)現(xiàn)計(jì)算兩個(gè)經(jīng)緯度之間距離的方法,結(jié)合實(shí)例形式對(duì)比分析了Go語言與php進(jìn)行經(jīng)緯度計(jì)算的相關(guān)數(shù)學(xué)運(yùn)算技巧,需要的朋友可以參考下2016-07-07