PHP methods to prevent website being attacked_PHP tutorial

WBOY
Release: 2016-07-13 17:09:35
Original
887 people have browsed it

Recently, the website has been frequently attacked. Then I thought of a way to use php to prevent the website from being attacked. The following is my code. The code is not the best. You can do it according to your own needs. Let’s take a look at my code.

Recently, the website has been frequently attacked. Then I thought of a way to use PHP tutorials to prevent the website from being attacked. The following is my code. The code is not the best. You can do it according to your own needs. Let’s take a look at my code. .
/*
*Website anti-ip attack code (anti-ip attack code website)2010-11-20, ver2.0
*mydalle.com anti-refresh mechanism
*design by www.bKjia.c0m
*/

//Query banned ip
$ip =$_server['remote_addr'];
$fileht=".htaccess2";
if(!file_exists($fileht))file_put_contents($fileht,"");
$filehtarr=@file($fileht);
if(in_array($ip."rn",$filehtarr))die("warning:"."
"."your ip address are forbided by mydalle.com anti-refresh mechanism, if you have any question pls emill to shop@mydalle.com!
(mydalle.com anti-refresh mechanism is to enable users to have a good shipping services, but there maybe some inevitable network problems in your ip address, so that you can mail to us to solve.)");

//Add banned IP
$time=time();
$fileforbid="log/forbidchk.dat";
if(file_exists($fileforbid))
{ if($time-filemtime($fileforbid)>30)unlink($fileforbid);
else{
$fileforbidarr=@file($fileforbid);
if($ip==substr($fileforbidarr[0],0,strlen($ip)))
{
if($time-substr($fileforbidarr[1],0,strlen($time))>120)unlink($fileforbid);
elseif($fileforbidarr[2]>120){file_put_contents($fileht,$ip."rn",file_append);unlink($fileforbid);}
else{$fileforbidarr[2]++;file_put_contents($fileforbid,$fileforbidarr);}
}
}
}
//Anti-refresh
$str="";
$file="log/ipdate.dat";
if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);
if(!file_exists($file))file_put_contents($file,"");
$allowtime = 60;//Anti-refresh time
$allownum=5;//Number of anti-refresh times
$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."1rn";
elseif($numtem<$allownum)$str.=$iptem.$uritem.$timetem.($numtem+1)."rn";
else
{
if(!file_exists($fileforbid)){$addforbidarr=array($ip."rn",time()."rn",1);file_put_contents($fileforbid,$addforbidarr);}
file_put_contents("log/forbided_ip.log",$ip."--".date("y-m-d h:i:s",time())."--".$uri."rn",file_append);
$timepass=$timetem+$allowtime-$time;
die("warning:"."
"."pls don't refresh too frequently, and wait for ".$timepass." seconds to continue, if not your ip address will be forbided automatically by mydalle.com anti -refresh mechanism!
(mydalle.com anti-refresh mechanism is to enable users to have a good shipping services, but there maybe some inevitable network problems in your ip address, so that you can mail to us to solve.) ");
}
}
}
}
if($yesno) $str.=$checkip.$checkuri.$time."1rn";
file_put_contents($file,$str);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629736.htmlTechArticleThe website has been frequently attacked recently, and then I thought of a way to use php to prevent the website from being attacked. Here is mine Code, the code is not the best, do it according to your own needs, here comes...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!