对IP及IP段进行访问限制

作者:网络 来源:佚名 更新时间:2008-10-18 21:22:25 点击:
本来是写了几个函数,ip黑名单是记到库里的,为了方便理解,改成了一个class把ip黑名单写了出来。

192.168.1.1 单个ip
192.168.1.*      这样代理   192.168.1.1-192.168.1-255
192.158.1.2-20 这样是代表192.158.1.2-192.158.1.20
也可以这样写  192.168.1.[1|2|3]

嘿嘿~一个方法不知道想法是否周全,拿出来大家讨论

使用
以下是引用片段:
$oblock_ip = new block_ip();
$oblock_ip->checkip();
以下是引用片段:
<?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);
       }

}
?>