php限制IP和IP段的代碼(白名單)

來源:互聯網
上載者:User

段代碼是我在網上搜相關解決方案時搜到的,這個類的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<=$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->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);
   }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.