php ddos 安全处理代码
<?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);
?>
https://www.cnblogs.com/wustart/archive/2017/07/24/7227791.html
相关文章
- PHP 安全问题入门:10 个常见安全问题 + 实例讲解
- php正则匹配中文
- PHP中使用Luhn算法校验信用卡及借记卡卡号
- 禁用PHP函数,可以对php.ini进行配置
- PHP安全相关的配置
- 腾讯PHP工程师面试题两份
- php中assert方法的安全问题
- php扩展中如何定义线程安全的全局对象
- 总结PHP-FPM与Nginx的通信机制
- PHP判断用户UA给搜索引擎和用户展示不同页面
- PHP代码安全有必要了解下
- php:PHP解析xml的4种方法
- php:如何使用PHP排序, key为字母+数字的数组(多维数组)
- php分享二十八:mysql运行中的问题排查
- php分享十一:PHP实现异步调用方法研究
- PHP抓取网络数据的6种常见方法
- PHP采集库-Snoopy.class.php
- PHP中的符号 ->、=> 和 :: 分别表示什么意思?
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- php:在linux上用sudo提升权限(centos 8 / PHP 7.4.2)
- PHP - 获取和设置include_path .
- PHP如何将进程作为守护进程
- PHP-5.4.4 安装 APC 启东php-fpm 提示
- WinCache Extension for PHP
- [php-src] 理解Php内核中的函数与INI
- Atitit.sql ast 表达式 语法树 语法 解析原理与实现 java php c#.net js python
- PHP:Laravel cast array json数据存数据库时unicode 编码问题和update更新不触发数据转换
- PHP PDO函数库具体解释
- FastAdmin 将 PHP 框架升级到 ThinkPHP 5.1
- php去除换行符的方法小结(PHP_EOL变量的使用)
- php验证是否是中文
- 统计php程序运行时间及设置PHP最长运行时间