PHP under the site to prevent IP attack code, Super practical
Source: Internet
Author: User
Today, I developed the following code, which is the first in a day to intercept 15 IP, the server load is normal.
Copy CodeThe code is as follows:
<?php
Query prohibits 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! ");
Add Prohibit 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);}
}
}
}
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 = 120;//anti-refresh Time
$allowNum =10;//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. " 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 a much more, Pls wait for ". $timepass." Too to Seconds ");
}
}
}
}
if ($yesno) $str. = $checkip. $checkuri. $time. " 1\r\n ";
File_put_contents ($file, $STR);
?>
Include the program into the head of the PHP file you want to make.
The program first determines whether the IP is in the prohibited list, if it exits;
Otherwise, if IP is in the monitor list, click More than 600 times within 10 minutes to add a banned list.
If you do not exceed the time and number of times plus 1, at the same time, monitor whether the IP on the same page to operate frequently.
Because it is the machine brush frequency, so the program does not have to judge.
There is also the time to update, can not use exclusive processing files.
Using pseudo static storage IP can also be an error.
This program still has the change and the improvement leeway, everybody also may refer to the QQ farm the demotion principle, basically the farm also according to this principle development.
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