PhpIP get City API (pure IP database)-php Tutorial

Source: Internet
Author: User
PhpIP retrieval City API (pure IP database)

  1. /**
  2. * Obtain the city name pure IP database based on the IP address
  3. * Edit: bbs.it-home.org
  4. */
  5. Function convertip ($ ip ){
  6. // IP data file path
  7. $ Dat_path = 'qqwry. Dat ';
  8. // Check the IP address
  9. If (! Filter_var ($ ip, FILTER_VALIDATE_IP )){
  10. Return 'IP Address error ';
  11. }
  12. // Open the IP data file
  13. If (! $ Fd = @ fopen ($ dat_path, 'RB ')){
  14. Return 'IP date file not exists or access denied ';
  15. }
  16. // Calculate the number of integers by decomposing IP addresses.
  17. $ IpNum = ip2long ($ ip );
  18. // Obtain the start and end locations of the IP address 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 the binary search method to search for matched IP records from the index records while ($ ip1num> $ ipNum | $ ip2num <$ ipNum ){
  25. $ Middle = intval ($ EndNum + $ BeginNum)/2 );
  26. // Read 4 bytes from the offset pointer to the index position
  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 an integer. if the data is negative, the power of 2 is added.
  34. $ Ip1num = implode ('', unpack ('L', $ ipData1 ));
  35. If ($ ip1num <0) $ ip1num + = pow (2, 32 ); // if the number of extracted long integers is greater than our IP address, modify the end position for the next loop if ($ ip1num> $ ipNum ){
  36. $ EndNum = $ Middle;
  37. Continue;
  38. }
  39. // Obtain the next index after obtaining 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 dizzy. if you do not understand it, read it 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. $ 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. // Return the result after the replacement operation is completed.
  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. ?>

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.