Home >Backend Development >PHP Tutorial >php IP Get City API (Innocent IP Database)

php IP Get City API (Innocent IP Database)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:55:581935browse
  1. /**
  2. * Get city name based on IP address Innocence IP database
  3. * edit: bbs.it-home.org
  4. */
  5. function convertip($ip) {
  6. //IP data file path
  7. $dat_path = 'QQWry.Dat';
  8. //Check IP address
  9. if(!filter_var($ip, FILTER_VALIDATE_IP)) {
  10. return 'IP Address Error';
  11. }
  12. //Open IP data file
  13. if(!$fd = @fopen($dat_path, 'rb')){
  14. return 'IP date file not exists or access denied';
  15. }
  16. //Decompose the IP for operation and get an integer number
  17. $ipNum = ip2long($ip);
  18. //Get the start and end position of the IP data index
  19. $DataBegin = fread($fd, 4);
  20. $DataEnd = fread($fd, 4);
  21. $ipbegin = implode('', unpack('L', $DataBegin));
  22. if($ipbegin $ipend = implode('', unpack('L', $DataEnd));
  23. if($ipend $ ipNum || $ip2num $Middle= intval(($EndNum + $BeginNum) / 2);
  24. //Offset the pointer to the index position and read 4 bytes
  25. fseek($fd, $ ipbegin + 7 * $Middle);
  26. $ipData1 = fread($fd, 4);
  27. if(strlen($ipData1) fclose($fd);
  28. return 'System Error';
  29. }
  30. //The extracted data is converted into a long integer. If the data is a negative number, add 2 to the 32nd power
  31. $ip1num = implode('', unpack('L', $ipData1));
  32. if($ip1num $ipNum) {
  33. $EndNum = $Middle;
  34. continue;
  35. }
  36. //Get the next index after fetching the previous index
  37. $DataSeek = fread($fd, 3);
  38. if(strlen($DataSeek) fclose($fd);
  39. return 'System Error';
  40. }
  41. $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
  42. fseek($fd, $DataSeek);
  43. $ipData2 = fread( $fd, 4);
  44. if(strlen($ipData2) fclose($fd);
  45. return 'System Error';
  46. }
  47. $ip2num = implode('', unpack('L', $ipData2));
  48. if($ip2num
  49. //Unknown prompt not found
  50. if($ip2num if($Middle = = $BeginNum) {
  51. fclose($fd);
  52. return 'Unknown';
  53. }
  54. $BeginNum = $Middle;
  55. }
  56. }
  57. //The following code is confusing to read. I don’t understand it. I’m interested. Slow reading
  58. $ipFlag = fread($fd, 1);
  59. if($ipFlag == chr(1)) {
  60. $ipSeek = fread($fd, 3);
  61. if(strlen($ipSeek) fclose($fd);
  62. return 'System Error';
  63. }
  64. $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
  65. fseek($fd, $ ipSeek);
  66. $ipFlag = fread($fd, 1);
  67. }
  68. if($ipFlag == chr(2)) {
  69. $AddrSeek = fread($fd, 3);
  70. if(strlen($AddrSeek ) fclose($fd);
  71. return 'System Error';
  72. }
  73. $ipFlag = fread($fd, 1);
  74. if($ipFlag == chr(2)) {
  75. $AddrSeek2 = fread($fd, 3);
  76. if(strlen($AddrSeek2) fclose($fd);
  77. return 'System Error';
  78. }
  79. $AddrSeek2 = implode('', unpack(' L', $AddrSeek2.chr(0)));
  80. fseek($fd, $AddrSeek2);
  81. } else {
  82. fseek($fd, -1, SEEK_CUR);
  83. }
  84. while(($char = fread ($fd, 1)) != chr(0))
  85. $ipAddr2 .= $char;
  86. $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
  87. fseek ($fd, $AddrSeek);
  88. while(($char = fread($fd, 1)) != chr(0))
  89. $ipAddr1 .= $char;
  90. } else {
  91. fseek($fd, - 1, SEEK_CUR);
  92. while(($char = fread($fd, 1)) != chr(0))
  93. $ipAddr1 .= $char;
  94. $ipFlag = fread($fd, 1);
  95. if ($ipFlag == chr(2)) {
  96. $AddrSeek2 = fread($fd, 3);
  97. if(strlen($AddrSeek2) fclose($fd);
  98. return 'System Error';
  99. }
  100. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  101. fseek($fd, $AddrSeek2);
  102. } else {
  103. fseek($fd, -1, SEEK_CUR);
  104. }
  105. while(($char = fread($fd, 1)) != chr(0)){
  106. $ipAddr2 .= $char;
  107. }
  108. }
  109. fclose($fd);
  110. //最后做相应的替换操作后返回结果
  111. if(preg_match('/http/i', $ipAddr2)) {
  112. $ipAddr2 = '';
  113. }
  114. $ipaddr = "$ipAddr1 $ipAddr2";
  115. $ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
  116. $ipaddr = preg_replace('/^s*/is', '', $ipaddr);
  117. $ipaddr = preg_replace('/s*$/is', '', $ipaddr);
  118. if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
  119. $ipaddr = 'Unknown';
  120. }
  121. return $ipaddr;
  122. }
  123. header("Content-type: text/html; charset=utf-8");
  124. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  125. if(isset($_GET['q'])){
  126. $ip = $_GET['q'];
  127. if(filter_var("http://".$ip,FILTER_VALIDATE_URL,FILTER_FLAG_HOST_REQUIRED)){
  128. $ip = gethostbyname($ip);
  129. }
  130. }
  131. echo "$ip,".mb_convert_encoding(convertip($ip),"utf-8","gb2312");
  132. ?>
复制代码


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