PHP to prevent Ddos,dns, cluster attack implementation code

Source: Internet
Author: User

<?PHP/*vim:set expandtab tabstop=4 shiftwidth=4:*/// +----------------------------------------------------------------------+// | PHP Version 5 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2004 the PHP Group |// +----------------------------------------------------------------------+// | This source file was subject to version 3.0 of the PHP license, |//| That's bundled with the "This" file LICENSE, and is |//| available through the world-wide-web at the following URL: |// | Http://www.php.net/license/3_0.txt.                                  |// | If you do not receive a copy of the PHP license and is unable to |//| Obtain it through the World-wide-web, please send a note to |// |               [Email protected] so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Authors:original Author <[email protected]> |// | Your Name <[email protected]> |// +----------------------------------------------------------------------+////$Id: $/** * Protect against DDoS, DNS, cluster attacks * edit www.cxyblog.com*///Query prohibit IP$ip = $_server['REMOTE_ADDR']; $fileht=". Htaccess2";if(!file_exists ($FILEHT)) file_put_contents ($fileht,""); $filehtarr=@file ($fileht);if(In_array ($ip."", $filehtarr)) Die ("Warning:" . "<br>" . "Your IP address is forbided by some reason and IF you had any question Pls emill to [email protected]!");//Add Forbidden IP$time =Time (); $fileforbid="Log/forbidchk.dat";if(File_exists ($fileforbid)) {if($time-filemtime ($fileforbid) > -) unlink ($fileforbid); Else{$fileforbidarr=@file ($fileforbid); if($ip = = substr ($fileforbidarr [0],0, strlen ($IP))) {            if($time-substr ($fileforbidarr [1],0, strlen ($time)) > -) unlink ($fileforbid); ElseIf ($fileforbidarr [2] > -{file_put_contents ($fileht, $ip."", 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= -;//anti-refresh time$allowNum =Ten;//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, +); $uritem= substr ($v, +, +); $timetem= substr ($v, -,Ten); $numtem= substr ($v, About); if($time-$timetem <$allowTime) {        if($iptem! = $checkip) $str. =$v; Else{$yesno=false; if($uritem! = $checkuri) $str. = $iptem. $checkuri. $time."1";ElseIf ($numtem < $allowNum) $str. = $iptem. $uritem. $timetem. ($numtem +1) ."";            Else {                if(!file_exists ($fileforbid)) {$addforbidarr=Array ($ip."",Time ()."",                        1                    );                File_put_contents ($fileforbid, $addforbidarr); } file_put_contents ("Log/forbided_ip.log", $ip."--". Date"y-m-d h:i:s", time ())."--". $uri."", file_append);$timepass = $timetem + $allowTime-$time; Die ("Warning:"."<br>"."Sorry,you is forbided by refreshing frequently too much, Pls wait for". $timepass."seconds to continue!"); }        }    }}if($yesno) $str. = $checkip. $checkuri. $time."1";file_put_contents ($file, $str);?>

Http://u.cxyblog.com/14/article-aid-206.html?utm_source=tuicool&utm_medium=referral

PHP to prevent Ddos,dns, cluster attack implementation code

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.