php白名单类

3

使用方法

1
2
3
4
5
6
$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() ){
  exit('您的IP为:'.$oBlock_ip->ip.'禁止访问');
}

allowIp类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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);
   }
}

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

共 3 条评论

  1. 回复

    另外,如果我要限址非白名单上的IP,在wordpress中如何实现?

    • 回复

      在wp当前模板的functions.php中加入以上代码就可以了.

  2. 回复

    这个在备案的时候应该用得上!

朽木进行回复 X

您的邮箱不会公开,当您的评论有新的回复时,会通过您填写的邮箱向您发送评论内容。 必填字段 *

为何看不到我发布的评论?

正在提交, 请稍候...