淺析PHP程序防止ddos,dns,集群服務(wù)器攻擊的解決辦法
更新時(shí)間:2013年06月18日 15:44:16 作者:
本篇文章是對(duì)PHP程序防止ddos,dns,集群服務(wù)器攻擊的解決辦法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
廢話(huà)不多說(shuō),上代碼
<?php
//查詢(xún)禁止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);
?>
復(fù)制代碼 代碼如下:
<?php
//查詢(xún)禁止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);
?>
您可能感興趣的文章:
- JWT + ASP.NET MVC時(shí)間戳防止重放攻擊詳解
- .NET添加時(shí)間戳防止重放攻擊
- PHP中防止SQL注入攻擊和XSS攻擊的兩個(gè)簡(jiǎn)單方法
- php防止CC攻擊代碼 php防止網(wǎng)頁(yè)頻繁刷新
- php防止sql注入示例分析和幾種常見(jiàn)攻擊正則表達(dá)式
- php實(shí)現(xiàn)cc攻擊防御和防止快速刷新頁(yè)面示例
- php防止網(wǎng)站被攻擊的應(yīng)急代碼
- PHP中通過(guò)語(yǔ)義URL防止網(wǎng)站被攻擊的方法分享
- PHP實(shí)現(xiàn)的防止跨站和xss攻擊代碼【來(lái)自阿里云】
- PHP防止注入攻擊實(shí)例分析
- PHP如何防止XSS攻擊與XSS攻擊原理的講解
- PHP基于timestamp和nonce實(shí)現(xiàn)的防止重放攻擊方案分析
相關(guān)文章
PHP使用 Imagick 擴(kuò)展實(shí)現(xiàn)圖片合成,圓角處理功能示例
這篇文章主要介紹了PHP使用 Imagick 擴(kuò)展實(shí)現(xiàn)圖片合成,圓角處理功能,結(jié)合具體實(shí)例形式分析了PHP使用 Imagick 擴(kuò)展的圖形處理、生成相關(guān)操作技巧,需要的朋友可以參考下2019-09-09Zend Studio 實(shí)用快捷鍵一覽表(精心整理)
以下是小編精心整理的Zend Studio實(shí)用快捷鍵。很有用哦!需要的朋友可以過(guò)來(lái)參考下2013-08-08php實(shí)現(xiàn)網(wǎng)頁(yè)上一頁(yè)下一頁(yè)翻頁(yè)過(guò)程詳解
這篇文章主要介紹了php實(shí)現(xiàn)網(wǎng)頁(yè)上一頁(yè)下一頁(yè)翻頁(yè)過(guò)程詳解,大致功能就是頁(yè)面只顯示幾條信息,按上一頁(yè)、下一頁(yè)切換內(nèi)容,當(dāng)顯示第一頁(yè)時(shí)上一頁(yè)和首頁(yè)選項(xiàng)不可選,當(dāng)頁(yè)面加載到最后一頁(yè)時(shí)下一頁(yè)和尾頁(yè)選項(xiàng)不可選,需要的朋友可以參考下2019-06-06The specified CGI application misbehaved by not returning a
The specified CGI application misbehaved by not returning a complete set of HTTP headers2011-03-03