浅析PHP程序防止ddos,dns,集群服务器攻击的解决办法


Posted in PHP onJune 18, 2013

废话不多说,上代码

<?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;//防刷新次数
$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 相关文章推荐
十天学会php之第八天
Oct 09 PHP
ThinkPHP采用GET方式获取中文参数查询无结果的解决方法
Jun 26 PHP
PHP中单引号与双引号的区别分析
Aug 19 PHP
PHP类的封装与继承详解
Sep 29 PHP
10款实用的PHP开源工具
Oct 23 PHP
无需数据库在线投票调查php代码
Jul 20 PHP
php原生导出excel文件的两种方法(推荐)
Nov 19 PHP
PHP实现接收二进制流转换成图片的方法
Jan 10 PHP
PHP简单获取上月、本月、近15天、近30天的方法示例
Jul 03 PHP
PHP中cookie知识点学习
May 06 PHP
PHP从尾到头打印链表实例讲解
Sep 27 PHP
django中的ajax组件教程详解
Oct 18 PHP
解析PHP计算页面执行时间的实现代码
Jun 18 #PHP
php实现简单洗牌算法
Jun 18 #PHP
php抽奖小程序的实现代码
Jun 18 #PHP
解析PHP生成静态html文件的三种方法
Jun 18 #PHP
解析PHP汉字转换拼音的类
Jun 18 #PHP
解析php中反射的应用
Jun 18 #PHP
使用Smarty 获取当前日期时间和格式化日期时间的方法详解
Jun 18 #PHP
You might like
关于BIG5-HKSCS的解决方法
2007/03/20 PHP
微信公众平台消息接口校验与消息接口响应实例
2014/12/23 PHP
WordPress中限制非管理员用户在文章后只能评论一次
2015/12/31 PHP
对比PHP对MySQL的缓冲查询和无缓冲查询
2016/07/01 PHP
php 类中的常量、静态属性、非静态属性的区别
2017/04/09 PHP
使用PHP+MySql实现微信投票功能实例代码
2017/09/29 PHP
PHP实现按之字形顺序打印二叉树的方法
2018/01/16 PHP
ThinkPHP框架下微信支付功能总结踩坑笔记
2019/04/10 PHP
Javascript与flash交互通信基础教程
2008/08/07 Javascript
用Mootools获得操作索引的两种方法分享
2011/12/12 Javascript
JavaScript 动态加载脚本和样式的方法
2015/04/13 Javascript
javascript实时获取鼠标坐标值并显示的方法
2015/04/30 Javascript
Angularjs过滤器使用详解
2016/05/25 Javascript
关于webuploader插件使用过程遇到的小问题
2016/11/07 Javascript
jQuery 改变P标签文本值方法
2018/02/24 jQuery
Vue CLI3中使用compass normalize的方法
2019/05/30 Javascript
[01:18:45]DOTA2-DPC中国联赛 正赛 DLG vs Dragon BO3 第三场2月1日
2021/03/11 DOTA
Python lambda和Python def区别分析
2014/11/30 Python
pymongo为mongodb数据库添加索引的方法
2015/05/11 Python
django基础之数据库操作方法(详解)
2017/05/24 Python
Python实现的堆排序算法原理与用法实例分析
2017/11/22 Python
Python编程给numpy矩阵添加一列方法示例
2017/12/04 Python
Python单元测试简单示例
2018/07/03 Python
python基于C/S模式实现聊天室功能
2019/01/09 Python
Python Flask框架扩展操作示例
2019/05/03 Python
爬虫代理池Python3WebSpider源代码测试过程解析
2019/12/20 Python
Django多层嵌套ManyToMany字段ORM操作详解
2020/05/19 Python
使用OpenCV对车道进行实时检测的实现示例代码
2020/06/19 Python
HTML5实现应用程序缓存(Application Cache)
2020/06/16 HTML / CSS
英国顶级足球鞋的领先零售商:Lovell Soccer
2019/08/27 全球购物
车间班组长的职责
2013/12/13 职场文书
2014年单位植树节活动方案
2014/03/23 职场文书
全国法制宣传日活动总结
2015/05/05 职场文书
幼儿园新生开学寄语
2015/05/27 职场文书
教你用python实现一个无界面的小型图书管理系统
2021/05/21 Python
JS轻量级函数式编程实现XDM二
2022/06/16 Javascript