首頁 >後端開發 >php教程 >php讀取純真ip資料庫的簡單例子

php讀取純真ip資料庫的簡單例子

WBOY
WBOY原創
2016-07-25 08:54:401529瀏覽
  1. /*--------------------------- -----------------------
  2. ip2address [qqwry.dat]
  3. --------------- -----------------------------------*/
  4. class ip {
  5. var $fh; //IP資料庫檔案句柄
  6. var $first; //第一個索引
  7. var $last; //最後一條索引
  8. var $total; //索引總數
  9. //建構子
  10. function __construct() {
  11. $this->fh = fopen('qqwry.dat', 'rb'); //qqwry.dat檔案
  12. $this->first = $this->getLong4( );
  13. $this->last = $this->getLong4();
  14. $this->total = ($this->last - $this->first) / 7; //每個索引7位元組
  15. }
  16. //檢查IP合法性
  17. function checkIp($ip) {
  18. $arr = explode('.',$ip);
  19. if(count($arr ) !=4 ) {
  20. return false;
  21. } else {
  22. for ($i=0; $i if ($arr[$i] '255') {
  23. return false;
  24. }
  25. }
  26. }
  27. return true;
  28. }
  29. function getLong4( ) {
  30. //讀取little-endian編碼的4個位元組轉換為長整數數
  31. $result = unpack('Vlong', fread($this->fh, 4));
  32. return $result['long'];
  33. }
  34. function getLong3() {
  35. //讀取little-endian編碼的3個位元組轉換為長整數型數
  36. $result = unpack('Vlong', fread($this->fh, 3).chr(0));
  37. return $result['long'];
  38. }
  39. //查詢資訊
  40. function getInfo($data = "") {
  41. $char = fread($this->fh, 1);
  42. while (ord($char) != 0) { //國家地區資訊以0結束
  43. $data .= $char;
  44. $char = fread($this->fh, 1);
  45. }
  46. return $data;
  47. } bbs.it-home.org
  48. //查詢地區資訊
  49. function getArea() {
  50. $byte = fread($this->fh, 1); //標誌位元組
  51. switch (ord($byte)) {
  52. case 0: $area = ''; break; //沒有地區資訊
  53. case 1: //地區被重定向
  54. fseek($this->fh, $this->getLong3());
  55. $area = $this->getInfo(); break;
  56. case 2: //地區被重定向
  57. fseek($this->fh, $this->getLong3());
  58. $area = $this->getInfo(); break;
  59. default: $area = $this->getInfo($byte); break; //地區沒有被重新導向
  60. }
  61. return $area ;
  62. }
  63. function ip2addr($ip) {
  64. if(!$this -> checkIp($ip)){
  65. return false;
  66. }
  67. $ip = pack( 'N', intval(ip2long($ip)));
  68. //二分查找
  69. $l = 0;
  70. $r = $this->total;
  71. while($l $m = floor(($l $r) / 2); //計算中間索引
  72. fseek($this->fh, $this->first $m * 7);
  73. $beginip = strrev(fread($this->fh, 4)); //中間索引的開始IP位址
  74. fseek($this->fh, $this->getLong3());
  75. $endip = strrev(fread($this->fh, 4)); //中間索引的結束IP位址
  76. if ($ip $r = $m - 1;
  77. } else {
  78. if ($ip > $endip) { //使用者的IP大於中間索引的結束IP位址時
  79. $l = $m 1;
  80. } else { //使用者IP在中間索引的IP範圍內時
  81. $findip = $this->first $m * 7;
  82. break;
  83. }
  84. }
  85. }
  86. //查詢國家地區資訊
  87. fseek($this->fh, $findip);
  88. $location['beginip'] = long2ip($this->getLong4()); / /用戶IP所在範圍的開始位址
  89. $offset = $this->getlong3();
  90. fseek($this->fh, $offset);
  91. $location['endip'] = long2ip( $this->getLong4()); //使用者IP所在範圍的結束位址
  92. $byte = fread($this->fh, 1); //標誌位元組
  93. switch (ord($byte) ) {
  94. case 1: //國家和區域資訊都被重定向
  95. $countryOffset = $this->getLong3(); //重定向位址
  96. fseek($this->fh, $countryOffset );
  97. $byte = fread($this->fh, 1); //標誌位元組
  98. switch (ord($byte)) {
  99. case 2: //國家資訊被二次重定向
  100. fseek($this->fh, $this->getLong3());
  101. $location['country'] = $this->getInfo();
  102. fseek($this->fh , $countryOffset 4);
  103. $location['area'] = $this->getArea();
  104. break;
  105. default: //國家資訊沒有二次重新導向
  106. $location ['country'] = $this->getInfo($byte);
  107. $location['area'] = $this->getArea();
  108. break;
  109. }
  110. break;
  111. case 2: //國家資訊被重新導向
  112. fseek($this->fh, $this->getLong3());
  113. $location['country'] = $this->getInfo() ;
  114. fseek($this->fh, $offset 8);
  115. $location['area'] = $this->getArea();
  116. break;
  117. default: //國家信息沒有被重新導向
  118. $location['country'] = $this->getInfo($byte);
  119. $location['area'] = $this->getArea();
  120. break;
  121. }
  122. //gb2312 to utf-8(去除無訊息時顯示的CZ88.NET)
  123. foreach ($location as $k => $v) {
  124. $location[$k] = str_replace(' CZ88.NET','',iconv('gb2312', 'utf-8', $v));
  125. }
  126. return $location;
  127. }
  128. //析構函數
  129. function __destruct() {
  130. fclose($this->fh);
  131. }
  132. }
  133. $ip = new ip();
  134. $addr = $ip -> ip2addr(' IP位址');
  135. print_r($addr);
  136. ?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn