Website is attacked PHP prevention method _php Tutorial

Source: Internet
Author: User
Recently the site is often attacked, and later thought of a use of PHP to prevent the site from attack, the following is my code, the code is not the best, according to their own needs to do, the following to see my code.

Recently the site is often attacked, and later thought of a PHP tutorial to prevent the site from attack, the following is my code, the code is not the best, according to their own needs to do, the following to see my code.
/*
* Web site 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 prohibit 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 is forbided by mydalle.com Anti-refresh mechanism and if you had any question pls emill to shop@mydalle.com !
(mydalle.com Anti-refresh mechanism is to enable the users to has a good shipping services, but there maybe some inevitable n etwork problems in your IP address, so (can mail to us to solve.));

Add Forbidden 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;//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 would be forbided Automatic by mydalle.com Anti-refresh mechanism!
(mydalle.com Anti-refresh mechanism is to enable the users to has a good shipping services, but there maybe some inevitable n etwork problems in your IP address, so (can mail to us to solve.));
}
}
}
}
if ($yesno) $str. = $checkip. $checkuri. $time. " 1rn ";
File_put_contents ($file, $STR);
?>

http://www.bkjia.com/PHPjc/629736.html www.bkjia.com true http://www.bkjia.com/PHPjc/629736.html techarticle recently the site is often attacked, and later thought of a use of PHP to prevent the site from attack, the following is my code, the code is not the best, according to their own needs to do, the following ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.