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

访客4年前黑客工具374
<?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实例

相关文章

京东图床api接口源码

<?php header("Content-type:text/html;charset=utf-8"); //jkch.net if (class_exists('CURLFile')) { ...

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

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

php js 实例分享实现显示网站运行时间 - 显示年月日时分秒

分享两个版本的网站运行时间代码  PHP版本 显示为 本站运行:3年9月10天 <?php date_default_timezone_set('Asia/Shanghai')...

EMLOG博客给导航加自定义字体图标[美化导航新方法]

分享一个思源常用的给emlog加字体图标的方法 起源,,几天前写一个律师模板在手机端有个导航,需要加字体图标如图 进入正题 给导航加字体图标 1.首先,先引入字体图标(如模板自带,请省略)...

bing每日一图做网站背景

bing图片,清晰而且精美,做背景是不错的选择,而且每日一图 ps:选择强迫症的读音 第一步:创建一个bing.php文件,放入下列代码 <?php $str=file_get_conte...

PHP网页查看信息输入密码

1.主要原理用到了php的post和if函数 if($_POST["password"] == right_pwd) 2.我们需要输入一次后某时间段不失效就用到了cookies, setcook...