Home >php教程 >PHP源码 >php限制IP和IP段的代码(白名单)

php限制IP和IP段的代码(白名单)

WBOY
WBOYOriginal
2016-06-08 17:22:461448browse

实现的原理比较简单只要获取用户IP然后再在我们黑名单库中验证一下当前IP是不是存在即可进行过滤操作了,具体例子如下。

<script>ec(2);</script>

段代码是我在网上搜相关解决方法时搜到的,这个类的makePregIP函数逻辑有点问题,我修改了下可以使用了。这个类得功能是允许白名单中的IP地址访问,如果要实现限制黑名单中的IP地址访问,简单修改下checkIP函数中的代码逻辑就可以了。

使用方法

 代码如下 复制代码

$allow_ip = array("192.168.1.1","210.10.2.1-20","222.34.4.*","127.0.0.1");
 
$oBlock_ip = new allowIp($allow_ip);
if( !$oBlock_ip->checkIP() ){
  echo '您的IP为:';
  echo $oBlock_ip->ip;
  exit('禁止访问');
}


allowIP类文件

 代码如下 复制代码

class allowIp {
 
    function __construct($allow_ip){
        if (empty($allow_ip)) {
          return false;
        }
        $this->allow_ip = $allow_ip;
        $this->ip = '';
 
    }
 
    private function makePregIP($str)
    { 
        if (strstr($str,"-")) {
 
            $aIP = explode(".",$str);
 
            foreach ($aIP as $k=>$v) {
                if (!strstr($v,"-")) {
                    $preg_limit .= $this->makePregIP($v);
                    $preg_limit .= ".";
                } else{
                    $aipNum = explode("-",$v);
                    for($i=$aipNum[0];$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->allow_ip) {
            $i = 1;
            foreach ($this->allow_ip as $k=>$v) {
                $ipaddres = $this->makePregIP($v);
 
                $ip = str_ireplace(".",".",$ipaddres);
                $ip = str_replace("*","[0-9]{1,3}",$ip);
                $ipaddres = "/".$ip."/";
                $ip_list[] = $ipaddres;
                $i++;
            }
        }
        return $ip_list;
    }
 
    public function checkIP() {
        $iptable = $this->getAllBlockIP();
        $IsJoined = false;
        //取得用户ip
        $Ip = $this->get_client_ip();
        $Ip = trim($Ip);
        //在白名单中
        if ($iptable) {
            foreach($iptable as $value) {
                if (preg_match("{$value}",$Ip)) {
                    $IsJoined = true;
                    break;
                }
            }
        }
        //不在白名单中
        if( !$IsJoined ){
            return false;
        }
        return true; 
    }
 
    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";
        $this->ip = $ip;
        return($ip);
   }
}

Statement:
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