• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

网站防御DDOS的PHP代码_php

php 搞代码 7年前 (2018-06-21) 98次浏览 已收录 0个评论

今天的主题是网站攻击与网站防护。这是我查到的资料,能够有效的拦截住我的DDOS 一个php代码

 

<?php  
//查询禁止IP  
$ip =$_SERVER['REMOTE_ADDR'];  
$fileht=".htaccess2";  
if(!file_exists($fileht))file_put_contents($fileht,"");  
$filehtarr=@file($fileht);  

http://www.gaodaima.com/50451.html网站防御DDOS的PHP代码_php

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 [email protected]!");  

//加入禁止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;//防刷新次数  
$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);  
?> 

欢迎大家阅读《网站防御DDOS的PHP代码_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:网站防御DDOS的PHP代码_php
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址