Simple example of PHP reading pure IP Database

Source: Internet
Author: User
Tags ord unpack
  1. /*--------------------------------------------------
  2. ip2address [Qqwry.dat]
  3. --------------------------------------------------*/
  4. Class IP {
  5. var $fh; IP database file handle
  6. var $first; First Index
  7. var $last; Last index
  8. var $total; Total indexes
  9. constructor function
  10. function __construct () {
  11. $this->fh = fopen (' Qqwry.dat ', ' RB '); Qqwry.dat file
  12. $this->first = $this->getlong4 ();
  13. $this->last = $this->getlong4 ();
  14. $this->total = ($this->last-$this->first)/7; 7 bytes per index
  15. }
  16. Check IP legitimacy
  17. function Checkip ($IP) {
  18. $arr = Explode ('. ', $IP);
  19. if (count ($arr)!=4) {
  20. return false;
  21. } else {
  22. for ($i =0; $i < 4; $i + +) {
  23. if ($arr [$i] < ' 0 ' | | $arr [$i] > ' 255 ') {
  24. return false;
  25. }
  26. }
  27. }
  28. return true;
  29. }
  30. function GetLong4 () {
  31. Read the Little-endian encoded 4 bytes into a long integer number
  32. $result = Unpack (' Vlong ', fread ($this->fh, 4));
  33. return $result [' Long '];
  34. }
  35. function GetLong3 () {
  36. Read the Little-endian encoded 3 bytes into a long integer number
  37. $result = Unpack (' Vlong ', fread ($this->fh, 3). chr (0));
  38. return $result [' Long '];
  39. }
  40. Query information
  41. function GetInfo ($data = "") {
  42. $char = Fread ($this->fh, 1);
  43. while (ORD ($char)! = 0) {//Country area information ends with 0
  44. $data. = $char;
  45. $char = Fread ($this->fh, 1);
  46. }
  47. return $data;
  48. } bbs.it-home.org
  49. Find area Information
  50. function Getarea () {
  51. $byte = Fread ($this->fh, 1); Flag byte
  52. Switch (ord ($byte)) {
  53. Case 0: $area = '; Break No area information
  54. Case 1://Region redirected
  55. Fseek ($this->fh, $this->getlong3 ());
  56. $area = $this->getinfo (); Break
  57. Case 2://Region redirected
  58. Fseek ($this->fh, $this->getlong3 ());
  59. $area = $this->getinfo (); Break
  60. Default: $area = $this->getinfo ($byte); Break Region is not redirected
  61. }
  62. return $area;
  63. }
  64. function Ip2addr ($IP) {
  65. if (! $this, Checkip ($IP)) {
  66. return false;
  67. }
  68. $ip = Pack (' N ', Intval (Ip2long ($IP)));
  69. Two-point Search
  70. $l = 0;
  71. $r = $this->total;
  72. while ($l <= $r) {
  73. $m = Floor (($l + $r)/2); Calculate Intermediate Index
  74. Fseek ($this->fh, $this->first + $m * 7);
  75. $beginip = Strrev (fread ($this->fh, 4)); The start IP address of the intermediate index
  76. Fseek ($this->fh, $this->getlong3 ());
  77. $endip = Strrev (fread ($this->fh, 4)); End IP address of intermediate index
  78. if ($ip < $beginip) {//user's IP is less than the start IP address of the intermediate index
  79. $r = $m-1;
  80. } else {
  81. if ($ip > $endip) {//user's IP is greater than the end IP address of the intermediate index
  82. $l = $m + 1;
  83. } else {//User IP is within the IP range of the intermediate index
  84. $findip = $this->first + $m * 7;
  85. Break
  86. }
  87. }
  88. }
  89. Find information about countries and regions
  90. Fseek ($this->fh, $findip);
  91. $location [' beginip '] = Long2ip ($this->getlong4 ()); Start address of the range where the user IP is located
  92. $offset = $this->getlong3 ();
  93. Fseek ($this->fh, $offset);
  94. $location [' endip '] = Long2ip ($this->getlong4 ()); End address of the range where the user IP is located
  95. $byte = Fread ($this->fh, 1); Flag byte
  96. Switch (ord ($byte)) {
  97. Case 1://country and region information are redirected
  98. $countryOffset = $this->getlong3 (); REDIRECT Address
  99. Fseek ($this->fh, $countryOffset);
  100. $byte = Fread ($this->fh, 1); Flag byte
  101. Switch (ord ($byte)) {
  102. Case 2://Country information redirected two times
  103. Fseek ($this->fh, $this->getlong3 ());
  104. $location [' country '] = $this->getinfo ();
  105. Fseek ($this->fh, $countryOffset + 4);
  106. $location [' area '] = $this->getarea ();
  107. Break
  108. Default://Country information not redirected two times
  109. $location [' country '] = $this->getinfo ($byte);
  110. $location [' area '] = $this->getarea ();
  111. Break
  112. }
  113. Break
  114. Case 2://country information is redirected
  115. Fseek ($this->fh, $this->getlong3 ());
  116. $location [' country '] = $this->getinfo ();
  117. Fseek ($this->fh, $offset + 8);
  118. $location [' area '] = $this->getarea ();
  119. Break
  120. Default://Country information is not redirected
  121. $location [' country '] = $this->getinfo ($byte);
  122. $location [' area '] = $this->getarea ();
  123. Break
  124. }
  125. gb2312 to Utf-8 (cz88.net displayed when no information is removed)
  126. foreach ($location as $k = = $v) {
  127. $location [$k] = Str_replace (' CZ88. NET ', ', Iconv (' gb2312 ', ' utf-8 ', $v));
  128. }
  129. return $location;
  130. }
  131. Destructors
  132. function __destruct () {
  133. Fclose ($this->fh);
  134. }
  135. }
  136. $ip = new IP ();
  137. $ADDR = ip2addr (' IP address '), $ip
  138. Print_r ($ADDR);
  139. ?>
Copy Code
  • Related Article

    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.