🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ /** * 获取请求ip * @return ip地址 */ function ip() { if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : ''; } ~~~ ~~~ /*CDN获取IP地址*/ function GetUserIP() { if (isset($_SERVER['HTTP_X_SHOPIFY_CLIENT_IP'])) { $ip = $_SERVER['HTTP_X_SHOPIFY_CLIENT_IP']; } else { if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { $ip = $_SERVER['HTTP_CF_CONNECTING_IP']; } else { if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } } } return $ip; } ~~~ ~~~ //获取IP function ip(){ if(!C('CDN_IP')){ return $_SERVER['REMOTE_ADDR']; }else{ foreach (['HTTP_CDN_SRC_IP','HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR'] as $v) { if(isset($_SERVER[$v])) return $_SERVER[$v]; } return $_SERVER['REMOTE_ADDR']; } } ~~~ ~~~ /** * 根据IP获取请求地区(太平洋IP库) * @param $ip * @return 所在位置 */ function get_address($ip){ if($ip == '127.0.0.1') return '本地地址'; $content = @file_get_contents('http://whois.pconline.com.cn/ipJson.jsp?ip='.$ip.'&json=true'); $content=iconv('GB2312', 'UTF-8', $content); $arr = json_decode($content, true); if(is_array($arr)&& $arr['regionCode']==0){ return $arr['addr']; }else{ return '未知'; } } /** * 根据IP获取请求地区(淘宝IP库,不能刷新太频繁,否则502) * @param $ip * @return 所在位置 */ function get_address_old($ip){ if($ip == '127.0.0.1') return '本地地址'; $content = @file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip); $arr = json_decode($content, true); if(is_array($arr) && $arr['code']==0){ return $arr['data']['country'].'-'.$arr['data']['region'].'-'.$arr['data']['city']; }else{ return '未知'; } } ~~~