Home  >  Article  >  Backend Development  >  Obtain client + server IP and geographical location information

Obtain client + server IP and geographical location information

WBOY
WBOYOriginal
2016-07-25 09:01:59950browse
Obtain client + server IP and geographical location information IP: 113.83.***.**
Return status: 1
IP range Strat: 113.83.0.0
IP Range End: 113.83.255.255
Country: China
Province: Guangdong
City: Huizhou
District/County:
Line: Telecom
Type:
Description:
  1. /**
  2. * Get client + server IP and geographical location information 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. //Get geographical location information
  10. function iplookup($ip=1){
  11. if($ip){ //Client
  12. $userip=egetip_joy(); // Client IP
  13. }else{ //Server
  14. $domain=$_SERVER['HTTP_HOST'];
  15. $userip=gethostbyname($domain);
  16. }
  17. //Return Sina geographical location information
  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); //Filter extra characters
  28. $json_arr=json_decode($json, true);
  29. return $json_arr;
  30. }
  31. //Get 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. * Simulate unescape in JS
  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) The parameter is not empty to obtain the server IP
  82. echo "IP:".egetip_joy()."
    ";
  83. echo 'Return status:'.$iplookup['ret']."
  84. echo 'IP range Strat:'.$iplookup['start']."
    ";
  85. echo 'IP rangeEnd:'.$iplookup['end']."
    " ;
  86. echo 'Country:'.unescape($iplookup['country'])."
    ";
  87. echo 'Province:'.unescape($iplookup['province'])."
    " ;
  88. echo 'City:'.unescape($iplookup['city'])."
    ";
  89. echo 'District/County:'.unescape($iplookup['district'])."
  90. echo 'Line:'.unescape($iplookup['isp'])."
    ";
  91. echo 'Type:'.$iplookup['type']."
    ";
  92. echo 'Description:'.$iplookup['desc']."
    ";
  93. //print_r($iplookup);
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn