php代码分享防止刷流量攻击

访客4年前黑客工具384
<?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);

原理大概就是检测IP写入,然后屏蔽IP达到防止刷流量的效果

标签: PHP实例

相关文章

PHP发短信实现方法

1要拼接接收的手机号和短信 public function sendcode()     {         $parpm = input();         $valist = $this-&g...

思源工具箱抖音api解析接口更新

<?php header('Access-Control-Allow-Origin:*'); header('Content-Type:text/json;charset=utf8'); /*...

实战PHP皮皮虾无水印解析接口

目标接口:https://h5.pipix.com/bds/webapi/item/detail/?item_id= id根据301跳转后截取item/ 和?中间的值 curl模拟设备获取数据 然...

Emlog实现主分类以及子分类显示该分类全部分类和当前分类高亮

本文是介绍:Emlog如何实现主分类以及子分类显示该分类全部分类和分类高亮 我们知道要查询子分类很简单,只需要写一下sql查询一下哪个分类的pid是主分类ID 但是此方法到了子分类就会不显示或者出错,...

Emlog今日文章时间加红 今日未发文则显示昨日时间加红

Emlog今日文章时间加红 只需要写一个判断,那如果今日未发文则显示昨日时间加红怎么实现呢? 首先我们需要判断一下今日是否发文 把代码放到模板下module.php中 &l...

新浪图片外链失效使用远程图片解决方案

这里提供一种临时处理的思路:既然新浪判断了Referer,我们伪造就是了 最后用API方式调用,例如:api.php?url=新浪外链地址 远程图片 <?php/**  * @descri...