get_client_ip() 获取IP地址
get_client_ip()获取ip地址,在开启IPv6协议的主机上会全部返回0.0.0.0原因是他会把ipv6地址认为是非法地址而转换成0.0.0.0,而ipv4地址在ipv6主机上用get_client_ip()会放回类似::ffff:192.168.1.250这样的形式。
1 /** 2 * 转换IPv6地址为bin 3 * @param string $ip 返回类型 0 数字 1 返回False 4 * @return mixed 5 */ 6 function ip2bin($ip) 7 { 8 if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) 9 return base_convert(ip2long($ip),10,2); 10 if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) 11 return false; 12 if(($ip_n = inet_pton($ip)) === false) return false; 13 $bits = 15; // 16 x 8 bit = 128bit (ipv6) 14 while ($bits >= 0) 15 { 16 $bin = sprintf("%08b",(ord($ip_n[$bits]))); 17 $ipbin = $bin.$ipbin; 18 $bits--; 19 } 20 return $ipbin; 21 } 22 /** 23 * 转换bin地址为IPv6 或IPv4 24 * @param long $bin 返回类型 0 IPv4 IPv6地址 25 * @return mixed 26 */ 27 function bin2ip($bin) 28 { 29 if(strlen($bin) <= 32) // 32bits (ipv4) 30 return long2ip(base_convert($bin,2,10)); 31 if(strlen($bin) != 128) 32 return false; 33 $pad = 128 - strlen($bin); 34 for ($i = 1; $i <= $pad; $i++) 35 { 36 $bin = "0".$bin; 37 } 38 $bits = 0; 39 while ($bits <= 7) 40 { 41 $bin_part = substr($bin,($bits*16),16); 42 $ipv6 .= dechex(bindec($bin_part)).":"; 43 $bits++; 44 } 45 return inet_ntop(inet_pton(substr($ipv6,0,-1))); 46 } 47 /** 48 * 获取客户端IP地址 49 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 50 * @return mixed 51 */ 52 function get_client_ip6($type = 0) { 53 $type = $type ? 1 : 0; 54 static $ip = NULL; 55 if ($ip !== NULL) return $ip[$type]; 56 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 57 $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); 58 $pos = array_search('unknown',$arr); 59 if(false !== $pos) unset($arr[$pos]); 60 $ip = trim($arr[0]); 61 }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { 62 $ip = $_SERVER['HTTP_CLIENT_IP']; 63 }elseif (isset($_SERVER['REMOTE_ADDR'])) { 64 $ip = $_SERVER['REMOTE_ADDR']; 65 } 66 // IP地址合法验证 67 $long = sprintf("%u",ip2bin($ip)); 68 $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); 69 return $ip[$type]; 70 }