封装的获取IP的函数
1 /** 2 * 获取IP 3 * strcasecmp 比较函数 4 */ 5 if (!function_exists('get_ip')) { 6 7 function get_ip($outType = 'int') { 8 if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { 9 $onlineip = getenv('HTTP_CLIENT_IP'); 10 } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { 11 $onlineip = getenv('HTTP_X_FORWARDED_FOR'); 12 } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { 13 $onlineip = getenv('REMOTE_ADDR'); 14 } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { 15 $onlineip = $_SERVER['REMOTE_ADDR']; 16 } 17 18 preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches); 19 $onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : false; 20 unset($onlineipmatches); 21 22 if ($outType == 'int') { 23 $onlineip = ip2long($onlineip); 24 if ($onlineip) { 25 $onlineip = (int)$onlineip; 26 } 27 } 28 29 return $onlineip; 30 } 31 32 }