PHP IP Get City API (pure IP database)

Source: Internet
Author: User
Tags fread get ip pow unpack
  1. /**
  2. * Get city name based on IP address pure 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. Decomposition IP for operation, resulting in shaping number
  17. $ipNum = Ip2long ($IP);
  18. Get IP Data index start and end locations
  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; Search for matching IP records from index records using binary lookup while ($ip 1num> $ipNum | | $ip 2num< $ipNum) {
  25. $Middle = Intval (($EndNum + $BeginNum)/2);
  26. Offset pointer to index position read 4 bytes
  27. Fseek ($FD, $ipbegin + 7 * $Middle);
  28. $ipData 1 = fread ($FD, 4);
  29. if (strlen ($ipData 1) < 4) {
  30. Fclose ($FD);
  31. Return ' System Error ';
  32. }
  33. Extracted data transformation growth shaping, if the data is negative, plus 2 of the power of 32
  34. $ip 1num = Implode (', Unpack (' L ', $ipData 1));
  35. if ($ip 1num < 0) $ip 1num + = POW (2, 32); Extract the number of long integers greater than our IP address then modify the end position to the next loop if ($ip 1num > $ipNum) {
  36. $EndNum = $Middle;
  37. Continue
  38. }
  39. Remove an index after the last index is taken
  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. $ipData 2 = fread ($FD, 4);
  48. if (strlen ($ipData 2) < 4) {
  49. Fclose ($FD);
  50. Return ' System Error ';
  51. }
  52. $ip 2num = Implode (', Unpack (' L ', $ipData 2));
  53. if ($ip 2num < 0) $ip 2num + = POW (2, 32);
  54. No hints found
  55. if ($ip 2num < $ipNum) {
  56. if ($Middle = = $BeginNum) {
  57. Fclose ($FD);
  58. Return ' Unknown ';
  59. }
  60. $BeginNum = $Middle;
  61. }
  62. }
  63. The following code read dizzy, did not read clearly, interested in reading slowly
  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. $AddrSeek 2 = fread ($FD, 3);
  84. if (strlen ($AddrSeek 2) < 3) {
  85. Fclose ($FD);
  86. Return ' System Error ';
  87. }
  88. $AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
  89. Fseek ($FD, $AddrSeek 2);
  90. } else {
  91. Fseek ($FD,-1, seek_cur);
  92. }
  93. while ($char = Fread ($FD, 1))! = Chr (0))
  94. $ipAddr 2. = $char;
  95. $AddrSeek = Implode (', Unpack (' L ', $AddrSeek. chr (0)));
  96. Fseek ($FD, $AddrSeek);
  97. while ($char = Fread ($FD, 1))! = Chr (0))
  98. $ipAddr 1. = $char;
  99. } else {
  100. Fseek ($FD,-1, seek_cur);
  101. while ($char = Fread ($FD, 1))! = Chr (0))
  102. $ipAddr 1. = $char;
  103. $ipFlag = Fread ($FD, 1);
  104. if ($ipFlag = = Chr (2)) {
  105. $AddrSeek 2 = fread ($FD, 3);
  106. if (strlen ($AddrSeek 2) < 3) {
  107. Fclose ($FD);
  108. Return ' System Error ';
  109. }
  110. $AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
  111. Fseek ($FD, $AddrSeek 2);
  112. } else {
  113. Fseek ($FD,-1, seek_cur);
  114. }
  115. while ($char = Fread ($FD, 1))! = Chr (0)) {
  116. $ipAddr 2. = $char;
  117. }
  118. }
  119. Fclose ($FD);
  120. The result is returned after the corresponding substitution operation is finally made
  121. if (Preg_match ('/http/i ', $ipAddr 2)) {
  122. $ipAddr 2 = ";
  123. }
  124. $IPADDR = "$ipAddr 1 $ipAddr 2";
  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. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.