淺析PHP程序防止ddos,dns,集群服務器攻擊的解決辦法
更新時間:2013年06月18日 15:44:16 作者:
本篇文章是對PHP程序防止ddos,dns,集群服務器攻擊的解決辦法進行了詳細的分析介紹,需要的朋友參考下
廢話不多說,上代碼
<?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);
?>
復制代碼 代碼如下:
<?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);
?>
您可能感興趣的文章:
- JWT + ASP.NET MVC時間戳防止重放攻擊詳解
- .NET添加時間戳防止重放攻擊
- PHP中防止SQL注入攻擊和XSS攻擊的兩個簡單方法
- php防止CC攻擊代碼 php防止網(wǎng)頁頻繁刷新
- php防止sql注入示例分析和幾種常見攻擊正則表達式
- php實現(xiàn)cc攻擊防御和防止快速刷新頁面示例
- php防止網(wǎng)站被攻擊的應急代碼
- PHP中通過語義URL防止網(wǎng)站被攻擊的方法分享
- PHP實現(xiàn)的防止跨站和xss攻擊代碼【來自阿里云】
- PHP防止注入攻擊實例分析
- PHP如何防止XSS攻擊與XSS攻擊原理的講解
- PHP基于timestamp和nonce實現(xiàn)的防止重放攻擊方案分析
相關文章
PHP使用 Imagick 擴展實現(xiàn)圖片合成,圓角處理功能示例
這篇文章主要介紹了PHP使用 Imagick 擴展實現(xiàn)圖片合成,圓角處理功能,結合具體實例形式分析了PHP使用 Imagick 擴展的圖形處理、生成相關操作技巧,需要的朋友可以參考下2019-09-09php實現(xiàn)網(wǎng)頁上一頁下一頁翻頁過程詳解
這篇文章主要介紹了php實現(xiàn)網(wǎ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