标签:serve for 架构 fun work 功能 int 白名单 private
config.php
//ip白名单配置 ‘ipWlist‘=>[ ‘ifFilter‘=>true, //是否开启白名单功能 ‘wlist‘=>[ ‘10.0.0.19‘, ], ‘warea1‘=>‘10.8.0.0/16‘, //白名单网段1 ‘warea2‘=>‘10.12.0.0/16‘, //白名单网段1 ],
commonfunc.php
private function checkIp(){ $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"]; $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"]; $ipC=config(‘appconf.ipWlist‘); if(!$ipC[‘ifFilter‘]){ return true; } if(in_array($user_IP, $ipC[‘wlist‘])){ return true; } if( ! $this->ip_in_network($user_IP, $ipC[‘warea1‘])){ if( ! $this->ip_in_network($user_IP, $ipC[‘warea2‘])){ return false; } } return true; } private function ip_in_network($ip, $network) { $ip = (double) (sprintf("%u", ip2long($ip))); $s = explode(‘/‘, $network); $network_start = (double) (sprintf("%u", ip2long($s[0]))); $network_len = pow(2, 32 - $s[1]); $network_end = $network_start + $network_len - 1; if ($ip >= $network_start && $ip <= $network_end) { return true; } return false; }
可以用json_encode,序列化,eval()等方式来解决,如下
define("IPFILTER",1); define(‘IPWLISTJSON‘,json_encode([‘127.0.0.1‘,]));
// 业务中
$wlist = json_decode(IPWLISTJSON,1);
define(‘IPWLIST‘,"return [‘127.0.0.1‘,];");
// 业务中
$wlist=eval(IPWLIST)
define(‘IPWLIST‘,serialize([‘127.0.0.1‘,]));
// 业务中 $wlist=unserialize(IPWLIST);
还可以用string特殊分隔符的explode形式等,这里就不一一举例了
标签:serve for 架构 fun work 功能 int 白名单 private
原文地址:https://www.cnblogs.com/bushuwei/p/12191240.html