获取客户端+服务端IP及地理位置信息
IP:113.83.***.**
返回状态:1
IP范围Strat:113.83.0.0
IP范围End:113.83.255.255
国家:中国
省份:广东
城市:惠州
区/县:
线路:电信
类型:
描写:
- /**
- * 获取客户端+服务端IP及地理位置信息 Sina iplookup
- *
- * @Support:QQ 910111100 (JoY)
- * @Time: 2012.10.11 15:50:00
- * @HZapi.com (http://www.hzapi.com/)
- *
- */
- //取得地理位置信息
- function iplookup($ip=1){
- if($ip){ //客户端
- $userip=egetip_joy(); //客户端IP
- }else{ //服务端
- $domain=$_SERVER['HTTP_HOST'];
- $userip=gethostbyname($domain);
- }
- //返回Sina地理位置信息
- $json=@file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='.$userip);
- $patterns = array();
- $patterns[0] = '/var remote_ip_info = /';
- $patterns[1] = '/;/';
- $patterns[2] = '/\Wu/';
- $find = array();
- $find[0] = "";
- $find[1] = "";
- $find[2] = "%u";
- $json = preg_replace($patterns, $find, $json); //过滤多余字符
- $json_arr=json_decode($json,true);
- return $json_arr;
- }
- //取得IP
- function egetip_joy(){
- 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'];
- }
- $ip=preg_replace("/^([d.]+).*/","1",$ip);
- return $ip;
- }
- /**
- * 模拟JS里的unescape
- *
- * @Support:QQ 910111100 (JoY)
- * @Time: 2012.09.29 15:50:00
- * @HZapi.com (http://www.hzapi.com/)
- * echo unescape('%u4e1c%u6e56%u82b1%u56ed4%u53f7%u5c0f%u533a');
- */
- function unescape($str) {
- $str = rawurldecode($str);
- preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/U",$str,$r);
- $ar = $r[0];
- foreach($ar as $k=>$v) {
- if(substr($v,0,2) == "%u")
- {
- $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
- }
- elseif(substr($v,0,3) == "")
- {
- $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
- }
- elseif(substr($v,0,2) == "")
- {
- echo substr($v,2,-1)."";
- $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
- }
- }
- return join("",$ar);
- }
-
-
- $iplookup=iplookup(); //iplookup(1)参数非空为获取服务器端IP
-
- echo "IP:".egetip_joy()."
";
- echo '返回状态:'.$iplookup['ret']."
";
- echo 'IP范围Strat:'.$iplookup['start']."
";
- echo 'IP范围End:'.$iplookup['end']."
";
- echo '国家:'.unescape($iplookup['country'])."
";
- echo '省份:'.unescape($iplookup['province'])."
";
- echo '城市:'.unescape($iplookup['city'])."
";
- echo '区/县:'.unescape($iplookup['district'])."
";
- echo '线路:'.unescape($iplookup['isp'])."
";
- echo '类型:'.$iplookup['type']."
";
- echo '描写:'.$iplookup['desc']."
";
- //print_r($iplookup);
复制代码
|