1 /**
2 * 获取客户端IP地址
3 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
4 * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
5 * @return mixed
6 */
7 public function ip($type = 0, $adv = true)
8 {
9 $type = $type ? 1 : 0;
10 static $ip = null;
11 if (null !== $ip) {
12 return $ip[$type];
13 }
14
15 $httpAgentIp = Config::get('http_agent_ip');
16
17 if ($httpAgentIp && isset($_SERVER[$httpAgentIp])) {
18 $ip = $_SERVER[$httpAgentIp];
19 } elseif ($adv) {
20 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
21 $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
22 $pos = array_search('unknown', $arr);
23 if (false !== $pos) {
24 unset($arr[$pos]);
25 }
26 $ip = trim(current($arr));
27 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
28 $ip = $_SERVER['HTTP_CLIENT_IP'];
29 } elseif (isset($_SERVER['REMOTE_ADDR'])) {
30 $ip = $_SERVER['REMOTE_ADDR'];
31 }
32 } elseif (isset($_SERVER['REMOTE_ADDR'])) {
33 $ip = $_SERVER['REMOTE_ADDR'];
34 }
35 // IP地址合法验证
36 $long = sprintf("%u", ip2long($ip));
37 $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
38 return $ip[$type];
39 }