首頁 >php教程 >php手册 >php判断ip黑名单程序代码

php判断ip黑名单程序代码

WBOY
WBOY原創
2016-05-25 16:45:031006瀏覽

学校的新闻系统要求有些新闻只开放校内ip浏览,于是重写了一个代码来实现此功能,实现后的结果是,只要把允许访问的ip列入ip.txt这个文件中即可,同时支持c类ip,例如:

ip.txt 
192.168 
211.67.188 
211.67.191.25

PHP实例代码如下:

<?php
/*
 * ip地址黑名单、白名单
 * 判断访客地址的ip是否在ip.txt中,支持c类ip
 * By xhat
*/
$ip = $_SERVER[&#39;REMOTE_ADDR&#39;];
$ipArray = preg_replace("#rn?|n#", "", file(&#39;ip.txt&#39;));
foreach ($ipArray as $ipTest) {
    if (substr_count($ip, $ipTest) != "0") {
        echo "ok"; //执行相关命令
        die();
    }
}
?>

上面大家可以使用代码来骗过了,下面代码进行升级:

<?php
class block_ip {
    var $Block_ip = array(
        "192.168.1.1",
        "210.10.2.1-20",
        "222.34.4.*"
    );
    function __construct() {
    }
    function __destruct() {
    }
    private function makePregIP($str) {
        if (strstr($str, "-")) {
            $aIP = explode(".", $str);
            foreach ($aIP as $k => $v) {
                if (!strstr($v, "-")) {
                    $preg_limit.= makePregIP($v);
                } else {
                    $aipNum = explode("-", $v);
                    for ($i = $aipNum[0]; $i <= $aipNum[1]; $i++) {
                        $preg.= $preg ? "|" . $i : "[" . $i;
                    }
                    $preg_limit.= strrpos($preg_limit, ".", 1) == (strlen($preg_limit) - 1) ? $preg . "]" : "." . $preg . "]";
                }
            }
        } else {
            $preg_limit.= $str . ".";
        }
        return $preg_limit;
    }
    private function getAllBlockIP() {
        if ($this->Block_ip) {
            foreach ($this->Block_ip as $k => $v) {
                $ipaddres = $this->makePregIP($v->start_ip);
                $ip = str_ireplace(".", ".", $ipaddres);
                $ip = str_replace("*", "[0-9]{1,3}", $ip);
                $ipaddres = "/" . $ip . "/";
                $ip_list[] = $ipaddres;
            }
        }
        return $ip_list;
    }
    public function checkIP() {
        $iptable = $this->getAllBlockIP();
        $IsJoined = true;
        //取得用户ip
        $Ip = $this->get_client_ip();
        $Ip = trim($Ip);
        //剔除黑名单中的IP区段
        if ($iptable) {
            foreach ($iptable as $value) {
                if (preg_match("{$value}", $Ip)) {
                    $IsJoined = false;
                    break;
                }
            }
        }
        // 如果在ip黑名单中就执行如下操作
        if (!$IsJoined) {
            echo "IP Error";
            exit;
        }
    }
    private function get_client_ip() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP") , "unknown")) $ip =

getenv("HTTP_CLIENT_IP");

        else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR") , "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR");

        else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR") , "unknown")) $ip = getenv("REMOTE_ADDR");

        else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR'];

        else $ip = "unknown";

        return ($ip);

    }

}

?>

引用片段,代码如下:

$oBlock_ip = new block_ip(); 
$oBlock_ip->checkIP();


教程链接:

随意转载~但请保留教程地址★

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn