Analysis of PHP programs to prevent ddos, dns, and cluster server attacks

Source: Internet
Author: User
This article provides a detailed analysis of solutions for PHP programs to prevent ddos, dns, and cluster server attacks. For more information, see

This article provides a detailed analysis of solutions for PHP programs to prevent ddos, dns, and cluster server attacks. For more information, see

Speaking of nonsense, Hong Kong virtual hosts, server space, and code

The Code is as follows:


// Query the forbidden IP Address
$ 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 :"."
"." Your IP address are forbided by some reason, IF you have any question Pls emill to shop@mydalle.com! ");
// Add a prohibited IP Address
$ 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; // number of anti-Refresh attempts
$ 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 :"."
"." 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 );
?>

, Hong Kong server

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.