php IP Get City API (Innocent IP Database)

WBOY
Release: 2016-07-25 08:55:58
Original
1798 people have browsed it
  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 < 0) $ipbegin += pow(2, 32);
  23. $ipend = implode('', unpack('L', $DataEnd));
  24. if($ipend < 0) $ipend += pow(2, 32); $ipAllNum = ($ipend - $ipbegin) / 7 + 1; $BeginNum = 0; $EndNum = $ipAllNum; //Use binary search method to search for matching IP records from the index records while($ip1num>$ ipNum || $ip2num<$ipNum) {
  25. $Middle= intval(($EndNum + $BeginNum) / 2);
  26. //Offset the pointer to the index position and read 4 bytes
  27. fseek($fd, $ ipbegin + 7 * $Middle);
  28. $ipData1 = fread($fd, 4);
  29. if(strlen($ipData1) < 4) {
  30. fclose($fd);
  31. return 'System Error';
  32. }
  33. //The extracted data is converted into a long integer. If the data is a negative number, add 2 to the 32nd power
  34. $ip1num = implode('', unpack('L', $ipData1));
  35. if($ip1num < 0 ) $ip1num += pow(2, 32); //If the extracted long integer is greater than our IP address, modify the end position for the next cycle if($ip1num > $ipNum) {
  36. $EndNum = $Middle;
  37. continue;
  38. }
  39. //Get the next index after fetching the previous index
  40. $DataSeek = fread($fd, 3);
  41. if(strlen($DataSeek) < 3) {
  42. fclose($fd);
  43. return 'System Error';
  44. }
  45. $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
  46. fseek($fd, $DataSeek);
  47. $ipData2 = fread( $fd, 4);
  48. if(strlen($ipData2) < 4) {
  49. fclose($fd);
  50. return 'System Error';
  51. }
  52. $ip2num = implode('', unpack('L', $ipData2));
  53. if($ip2num < 0) $ip2num += pow(2, 32);
  54. //Unknown prompt not found
  55. if($ip2num < $ipNum) {
  56. if($Middle = = $BeginNum) {
  57. fclose($fd);
  58. return 'Unknown';
  59. }
  60. $BeginNum = $Middle;
  61. }
  62. }
  63. //The following code is confusing to read. I don’t understand it. I’m interested. Slow reading
  64. $ipFlag = fread($fd, 1);
  65. if($ipFlag == chr(1)) {
  66. $ipSeek = fread($fd, 3);
  67. if(strlen($ipSeek) < 3 ) {
  68. fclose($fd);
  69. return 'System Error';
  70. }
  71. $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
  72. fseek($fd, $ ipSeek);
  73. $ipFlag = fread($fd, 1);
  74. }
  75. if($ipFlag == chr(2)) {
  76. $AddrSeek = fread($fd, 3);
  77. if(strlen($AddrSeek ) < 3) {
  78. fclose($fd);
  79. return 'System Error';
  80. }
  81. $ipFlag = fread($fd, 1);
  82. if($ipFlag == chr(2)) {
  83. $AddrSeek2 = fread($fd, 3);
  84. if(strlen($AddrSeek2) < 3) {
  85. fclose($fd);
  86. return 'System Error';
  87. }
  88. $AddrSeek2 = implode('', unpack(' L', $AddrSeek2.chr(0)));
  89. fseek($fd, $AddrSeek2);
  90. } else {
  91. fseek($fd, -1, SEEK_CUR);
  92. }
  93. while(($char = fread ($fd, 1)) != chr(0))
  94. $ipAddr2 .= $char;
  95. $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
  96. fseek ($fd, $AddrSeek);
  97. while(($char = fread($fd, 1)) != chr(0))
  98. $ipAddr1 .= $char;
  99. } else {
  100. fseek($fd, - 1, SEEK_CUR);
  101. while(($char = fread($fd, 1)) != chr(0))
  102. $ipAddr1 .= $char;
  103. $ipFlag = fread($fd, 1);
  104. if ($ipFlag == chr(2)) {
  105. $AddrSeek2 = fread($fd, 3);
  106. if(strlen($AddrSeek2) < 3) {
  107. fclose($fd);
  108. return 'System Error';
  109. }
  110. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  111. fseek($fd, $AddrSeek2);
  112. } else {
  113. fseek($fd, -1, SEEK_CUR);
  114. }
  115. while(($char = fread($fd, 1)) != chr(0)){
  116. $ipAddr2 .= $char;
  117. }
  118. }
  119. fclose($fd);
  120. //最后做相应的替换操作后返回结果
  121. if(preg_match('/http/i', $ipAddr2)) {
  122. $ipAddr2 = '';
  123. }
  124. $ipaddr = "$ipAddr1 $ipAddr2";
  125. $ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
  126. $ipaddr = preg_replace('/^s*/is', '', $ipaddr);
  127. $ipaddr = preg_replace('/s*$/is', '', $ipaddr);
  128. if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
  129. $ipaddr = 'Unknown';
  130. }
  131. return $ipaddr;
  132. }
  133. header("Content-type: text/html; charset=utf-8");
  134. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  135. if(isset($_GET['q'])){
  136. $ip = $_GET['q'];
  137. if(filter_var("http://".$ip,FILTER_VALIDATE_URL,FILTER_FLAG_HOST_REQUIRED)){
  138. $ip = gethostbyname($ip);
  139. }
  140. }
  141. echo "$ip,".mb_convert_encoding(convertip($ip),"utf-8","gb2312");
  142. ?>
复制代码


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!