取得客戶端+服務端IP及地理位置信息

WBOY
發布: 2016-07-25 09:01:59
原創
953 人瀏覽過
取得客戶端+服務端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 * @HZapi.com (http://www.hzapi.com/)
  6. *
  7. */
  8. //取得地理位置資訊
  9. function iplookup($ip=1){
  10. if($ip){ //客戶端
  11. $userip=egetip_joy(); //客戶端IP
  12. }else{ //服務端
  13. $domain=$_SERVER['HTTP_HOST'];
  14. $ip=gethostbynameuserip=gethostbynameuserip=gethostbyname ($domain);
  15. }
  16. //回傳Sina地理位置資訊
  17. $json=@file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format =js&ip='.$userip);
  18. $patterns = array();
  19. $patterns[0] = '/var remote_ip_info = /';
  20. $patterns[1] = '/;/' ;
  21. $patterns[2] = '/Wu/';
  22. $find = array();
  23. $find[0] = "";
  24. $find[1] = "";
  25. $find[2] = "%u";
  26. $json = preg_replace($patterns, $find, $json); //過濾多餘字元
  27. $json_arr=json_decode($json,true) ;
  28. return $json_arr;
  29. }
  30. //取得IP
  31. function egetip_joy(){
  32. if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('IPTP_NLI)('HT) ))
  33. {
  34. $ip=getenv('HTTP_CLIENT_IP');
  35. }
  36. elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_FORWARDED_FOR')&&strcasecmp(getenv('HTy_FORW)> > {
  37. $ip=getenv('HTTP_X_FORWARDED_FOR');
  38. }
  39. elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown')&strcasecmp(getenv('REMOTE_ADDR'),'unknown'),
  40. > $ip=getenv('REMOTE_ADDR');
  41. }
  42. elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADn'. '))
  43. {
  44. $ip=$_SERVER['REMOTE_ADDR'];
  45. }
  46. $ip=preg_replace("/^([d.]+).*/","1 ",$ip);
  47. return $ip;
  48. }
  49. /**
  50. * 模擬JS裡的unescape
  51. *
  52. * @Support:QQ 910111100 (JoY)
  53. * @Time: 2012.09.29 15:50:00
  54. * @HZapi.com ://www.hzapi.com/)
  55. * echo unescape('%u4e1c%u6e56%u82b1%u56ed4%u53f7%u5c0f%u533a');
  56. */
  57. function unescape($str) {
  58. $str = rawurldecode($str);
  59. preg_match_all("/(?:%u.{4})|.{4};|d+;|.+/U",$str,$r);
  60. $ar = $r [0];
  61. foreach($ar as $k=>$v) {
  62. if(substr($v,0,2) == "%u")
  63. {
  64. $ar [$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
  65. }
  66. elseif(substr( $v,0,3) == "")
  67. {
  68. $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v ,3,-1)));
  69. }
  70. elseif(substr($v,0,2) == "")
  71. {
  72. echo substr($v,2,- 1)."";
  73. $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
  74. }
  75. }
  76. return join("",$ar);
  77. }
  78. $iplookup=iplookup(); //iplookup(1)參數非空為取得伺服器端IP
  79. echo "IP:".egetip_joy()."
    ";
  80. echo '回傳狀態:'.$iplookup['ret']."
    ";
  81. echo 'IP範圍Strat:'.$iplookup['start']."
    ";
  82. echo 'IP範圍End:'.$iplookup['end']."
    ";
  83. echo '國家:'.unescape($iplookup['country'])."
    ";
  84. echo '省:'.unescape($iplookup['province'])."
    ";
  85. echo '城市:'.unescape($iplookup['city'])."
    ";
  86. echo '區/縣:'.unescape($iplookup['district']). "
    ";
  87. echo '線路:'.unescape($iplookup['isp'])."
    ";
  88. echo '類型:'.$iplookup['type']."
    ";
  89. echo '描寫:'.$iplookup['desc']."
    ";
  90. //print_r($iplookup);
  91. 複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板