php IP黑名单

180it 2021-09-07 AM 1788℃ 0条
<?php 
//禁用ip地址 =====================================
$ip=getIP() ; 
//echo 'IP:'.$ip.'<br/>';

$file_path = "ip.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);

for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
//echo $file_arr[$i]."=<br />";
if(trim($file_arr[$i])==trim($ip)){ 
Header("Location: https://www.sti.org.cn/"); //黑名单ip自动跳转该url
die();
}
    }
}
//禁用ip地址 =====================================





function getip() {
    static $ip = '';
    if (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    if (isset($_SERVER['HTTP_CDN_SRC_IP'])) {
        $ip = $_SERVER['HTTP_CDN_SRC_IP'];
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
        foreach ($matches[0] as $xip) {
            if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                $ip = $xip;
                break;
            }
        }
    }
    if (preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $ip)) {
        return $ip;
    } else {
        return '127.0.0.1';
    }
}

//禁用ip地址 =====================================
?>
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php IP黑名单