淺析PHP程序防止ddos,dns,集群服務(wù)器攻擊的解決辦法
更新時間:2013年06月18日 15:44:16 作者:
本篇文章是對PHP程序防止ddos,dns,集群服務(wù)器攻擊的解決辦法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
廢話不多說,上代碼
<?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);
?>
復(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;//防刷新時間
$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注入示例分析和幾種常見攻擊正則表達(dá)式
- php實現(xiàn)cc攻擊防御和防止快速刷新頁面示例
- php防止網(wǎng)站被攻擊的應(yīng)急代碼
- PHP中通過語義URL防止網(wǎng)站被攻擊的方法分享
- PHP實現(xiàn)的防止跨站和xss攻擊代碼【來自阿里云】
- PHP防止注入攻擊實例分析
- PHP如何防止XSS攻擊與XSS攻擊原理的講解
- PHP基于timestamp和nonce實現(xiàn)的防止重放攻擊方案分析
相關(guān)文章
PHP設(shè)計模式之 策略模式Strategy詳解【對象行為型】
這篇文章主要介紹了PHP設(shè)計模式之 策略模式Strategy,結(jié)合實例形式詳細(xì)分析了PHP基于對象行為型的策略模式Strategy具體原理、實現(xiàn)技巧與相關(guān)操作注意事項,需要的朋友可以參考下2020-05-05
PHP和Shell實現(xiàn)檢查SAMBA與NFS Server是否存在
這篇文章主要介紹了PHP和Shell實現(xiàn)檢查SAMBA與NFS Server是否存在,本文分別給出了PHP檢查腳本和Shell檢查腳本,需要的朋友可以參考下2015-01-01
php下關(guān)于中英數(shù)字混排的字符串分割問題
要用到短信平臺的緣故,短信每條又有字?jǐn)?shù)的限制,而短信平臺的服務(wù)端又不會自己分析。短信平臺的后臺會自己分割,但api卻有沒這個功能。2010-04-04

