Home > Backend Development > PHP Tutorial > Simple example of php reading innocent ip database

Simple example of php reading innocent ip database

WBOY
Release: 2016-07-25 08:54:40
Original
1500 people have browsed it
  1. /*----------------------------------------- ----------
  2. ip2address [qqwry.dat]
  3. --------------------------------- --------------------*/
  4. class ip {
  5. var $fh; //IP database file handle
  6. var $first; //First index
  7. var $last; //The 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; //Each index is 7 bytes
  15. }
  16. //Check the legality of the IP
  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 little -Convert 4 bytes of endian encoding to long integer
  32. $result = unpack('Vlong', fread($this->fh, 4));
  33. return $result['long'];
  34. }
  35. function getLong3() {
  36. //Read the 3 bytes of little-endian encoding and convert it into a long integer
  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 and region information ends with 0
  44. $data .= $char;
  45. $char = fread($this->fh, 1);
  46. }
  47. return $data;
  48. } bbs.it-home.org
  49. //Query area information
  50. function getArea() {
  51. $byte = fread($this->fh, 1); //Flag byte
  52. switch (ord($byte)) {
  53. case 0: $area = ''; break; //There is no area information
  54. case 1: //The area is redirected
  55. fseek($this->fh, $this->getLong3());
  56. $ area = $this->getInfo(); break;
  57. case 2: //Area is redirected
  58. fseek($this->fh, $this->getLong3());
  59. $area = $this- >getInfo(); break;
  60. default: $area = $this->getInfo($byte); break; //The area 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. //Binary search
  70. $ l = 0;
  71. $r = $this->total;
  72. while($l <= $r) {
  73. $m = floor(($l + $r) / 2); //Calculate the intermediate index
  74. fseek($this->fh, $this->first + $m * 7);
  75. $beginip = strrev(fread($this->fh, 4)); //The starting IP address of the intermediate index
  76. fseek($this->fh, $this->getLong3());
  77. $endip = strrev(fread($this->fh, 4)); //End IP address of the intermediate index
  78. if ($ ip < $beginip) { //When the user's IP is less than the starting IP address of the intermediate index
  79. $r = $m - 1;
  80. } else {
  81. if ($ip > $endip) { //The user's IP is greater than When the ending IP address of the intermediate index is
  82. $l = $m + 1;
  83. } else { //When the user IP is within the IP range of the intermediate index
  84. $findip = $this->first + $m * 7;
  85. break ;
  86. }
  87. }
  88. }
  89. //Query country and region information
  90. fseek($this->fh, $findip);
  91. $location['beginip'] = long2ip($this->getLong4()); / /Start address of the user IP range
  92. $offset = $this->getlong3();
  93. fseek($this->fh, $offset);
  94. $location['endip'] = long2ip($this-> ;getLong4()); //End address of the user IP range
  95. $byte = fread($this->fh, 1); //Flag byte
  96. switch (ord($byte)) {
  97. case 1: //Both 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 is redirected twice
  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 is not redirected twice
  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 (remove CZ88.NET displayed when there is no information)
  126. foreach ($location as $k => $v) {
  127. $location[$k] = str_replace('CZ88.NET', '',iconv('gb2312', 'utf-8', $v));
  128. }
  129. return $location;
  130. }
  131. //Destructor
  132. function __destruct() {
  133. fclose($this->fh );
  134. }
  135. }
  136. $ip = new ip();
  137. $addr = $ip -> ip2addr('IP address');
  138. print_r($addr);
  139. ?>
Copy code


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