php ip黑名单 日拦截日志功能

180it 2021-09-07 AM 1693℃ 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)){ 
writelog(date("Y-m-d H:i:s",time()).' '.$ip.' '.$_SERVER['HTTP_HOST'].' 拦截成功');
Header("Location: https://www.sti.org.cn/");
die();
}
    }
}


function writelog($str)
{
$open=fopen(date("Ymd",time())."_log.txt","a" );
fwrite($open,$str.PHP_EOL);
fclose($open);
} 

//禁用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黑名单 日拦截日志功能