Home php教程 PHP源码 The function of automatically blocking the other party's IP when being attacked by CC or DDOS

The function of automatically blocking the other party's IP when being attacked by CC or DDOS

Nov 09, 2016 am 09:25 AM

This function is used to count how many times each visitor has visited in a short period of time. If the number of visits exceeds the limit, it returns TRUE. After that, you can use PHP to call linux's iptables to perform the blocking operation. I have used several I have actually tested DDOS tools and the results are very good.

By the way, by the way, I use files in the code to record the visitor IP and time. It is best not to use a database (and don’t be clever and save it in the session) ), and it’s best to put this file on the SSD hard drive. I won’t go into details about the reason. I guess everyone knows it

/**
    防止客戶端惡意重整
    用法:
        $isf5=Fun::isf5();
    返回:
        返回bool[true:對方在惡意重整;false:正常訪問]
/**/
public static function isf5(){
    $_f=Run.'_isf5';
    if(!file_exists($_f)){
        file_put_contents($_f,serialize(array()),LOCK_EX);
        chmod($_f,0777);
    }
    $arr=unserialize(file_get_contents($_f));
    $arr=(!is_array($arr)) ? array() : $arr;
 
    //清理掉10秒前訪問的用戶
    foreach($arr as $k=>$v){
        if($_ENV['now']-$v['t'] >= 10){
            unset($arr[$k]);
        }
    }
 
    $ip='_'.(self::cur('ip'));
 
    if(!isset($arr[$ip])){
        $arr[$ip]['n']=1;                       //1s內連線的次數
        $arr[$ip]['t']=$_ENV['now'];            //第1次訪問的時間
        file_put_contents($_f,serialize($arr),LOCK_EX);
        chmod($_f,0777);
        unset($_f,$arr,$ip);
        return FALSE;
    }else{
        if(!isset($arr[$ip]['t']) or !is_numeric($arr[$ip]['t'])){
            unset($arr[$ip]);
            file_put_contents($_f,serialize($arr),LOCK_EX);
            chmod($_f,0777);
            unset($_f,$arr,$ip);
            return FALSE;
        }
        if(($_ENV[&#39;now&#39;]-$arr[$ip][&#39;t&#39;]) <= 1){  //若距離上次訪問的時間沒有超過1s,則只累加次數
            $arr[$ip][&#39;n&#39;]+=1;
            if($arr[$ip][&#39;n&#39;]>=5){
                unset($_f,$arr,$ip);
                return TRUE;
            }else{
                file_put_contents($_f,serialize($arr),LOCK_EX);
                chmod($_f,0777);
                unset($_f,$arr,$ip);
                return FALSE;
            }
        }else{                                              //若距離上次訪問的時間已經超過1s,則重新計數
            $arr[$ip][&#39;n&#39;]=1;
            $arr[$ip][&#39;t&#39;]=$_ENV[&#39;now&#39;];
            file_put_contents($_f,serialize($arr),LOCK_EX);
            chmod($_f,0777);
            unset($_f,$arr,$ip);
            return FALSE;
        }
    }
    unset($_f,$arr,$ip);
    return FALSE;
}
Copy after login


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1254
24