当前位置:Gxlcms > PHP教程 > 获取客户端+服务端IP及地理位置信息

获取客户端+服务端IP及地理位置信息

时间:2021-07-01 10:21:17 帮助过:17人阅读

获取客户端+服务端IP及地理位置信息 IP:113.83.***.**
返回状态:1
IP范围Strat:113.83.0.0
IP范围End:113.83.255.255
国家:中国
省份:广东
城市:惠州
区/县:
线路:电信
类型:
描写:
  1. /**
  2. * 获取客户端+服务端IP及地理位置信息 Sina iplookup
  3. *
  4. * @Support:QQ 910111100 (JoY)
  5. * @Time: 2012.10.11 15:50:00
  6. * @HZapi.com (http://www.hzapi.com/)
  7. *
  8. */
  9. //取得地理位置信息
  10. function iplookup($ip=1){
  11. if($ip){ //客户端
  12. $userip=egetip_joy(); //客户端IP
  13. }else{ //服务端
  14. $domain=$_SERVER['HTTP_HOST'];
  15. $userip=gethostbyname($domain);
  16. }
  17. //返回Sina地理位置信息
  18. $json=@file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='.$userip);
  19. $patterns = array();
  20. $patterns[0] = '/var remote_ip_info = /';
  21. $patterns[1] = '/;/';
  22. $patterns[2] = '/\Wu/';
  23. $find = array();
  24. $find[0] = "";
  25. $find[1] = "";
  26. $find[2] = "%u";
  27. $json = preg_replace($patterns, $find, $json); //过滤多余字符
  28. $json_arr=json_decode($json,true);
  29. return $json_arr;
  30. }
  31. //取得IP
  32. function egetip_joy(){
  33. if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown'))
  34. {
  35. $ip=getenv('HTTP_CLIENT_IP');
  36. }
  37. elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown'))
  38. {
  39. $ip=getenv('HTTP_X_FORWARDED_FOR');
  40. }
  41. elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown'))
  42. {
  43. $ip=getenv('REMOTE_ADDR');
  44. }
  45. elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown'))
  46. {
  47. $ip=$_SERVER['REMOTE_ADDR'];
  48. }
  49. $ip=preg_replace("/^([d.]+).*/","1",$ip);
  50. return $ip;
  51. }
  52. /**
  53. * 模拟JS里的unescape
  54. *
  55. * @Support:QQ 910111100 (JoY)
  56. * @Time: 2012.09.29 15:50:00
  57. * @HZapi.com (http://www.hzapi.com/)
  58. * echo unescape('%u4e1c%u6e56%u82b1%u56ed4%u53f7%u5c0f%u533a');
  59. */
  60. function unescape($str) {
  61. $str = rawurldecode($str);
  62. preg_match_all("/(?:%u.{4})|.{4};|&#\d+;|.+/U",$str,$r);
  63. $ar = $r[0];
  64. foreach($ar as $k=>$v) {
  65. if(substr($v,0,2) == "%u")
  66. {
  67. $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
  68. }
  69. elseif(substr($v,0,3) == "")
  70. {
  71. $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
  72. }
  73. elseif(substr($v,0,2) == "&#")
  74. {
  75. echo substr($v,2,-1)."";
  76. $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
  77. }
  78. }
  79. return join("",$ar);
  80. }
  81. $iplookup=iplookup(); //iplookup(1)参数非空为获取服务器端IP
  82. echo "IP:".egetip_joy()."
    ";
  83. echo '返回状态:'.$iplookup['ret']."
    ";
  84. echo 'IP范围Strat:'.$iplookup['start']."
    ";
  85. echo 'IP范围End:'.$iplookup['end']."
    ";
  86. echo '国家:'.unescape($iplookup['country'])."
    ";
  87. echo '省份:'.unescape($iplookup['province'])."
    ";
  88. echo '城市:'.unescape($iplookup['city'])."
    ";
  89. echo '区/县:'.unescape($iplookup['district'])."
    ";
  90. echo '线路:'.unescape($iplookup['isp'])."
    ";
  91. echo '类型:'.$iplookup['type']."
    ";
  92. echo '描写:'.$iplookup['desc']."
    ";
  93. //print_r($iplookup);

人气教程排行