当前位置:Gxlcms > PHP教程 > php读取纯真ip数据库的简单例子

php读取纯真ip数据库的简单例子

时间:2021-07-01 10:21:17 帮助过:30人阅读

  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 < 4; $i++) {
  23. if ($arr[$i] <'0' || $arr[$i] > '255') {
  24. return false;
  25. }
  26. }
  27. }
  28. return true;
  29. }
  30. function getLong4() {
  31. //读取little-endian编码的4个字节转化为长整型数
  32. $result = unpack('Vlong', fread($this->fh, 4));
  33. return $result['long'];
  34. }
  35. function getLong3() {
  36. //读取little-endian编码的3个字节转化为长整型数
  37. $result = unpack('Vlong', fread($this->fh, 3).chr(0));
  38. return $result['long'];
  39. }
  40. //查询信息
  41. function getInfo($data = "") {
  42. $char = fread($this->fh, 1);
  43. while (ord($char) != 0) { //国家地区信息以0结束
  44. $data .= $char;
  45. $char = fread($this->fh, 1);
  46. }
  47. return $data;
  48. } bbs.it-home.org
  49. //查询地区信息
  50. function getArea() {
  51. $byte = fread($this->fh, 1); //标志字节
  52. switch (ord($byte)) {
  53. case 0: $area = ''; break; //没有地区信息
  54. case 1: //地区被重定向
  55. fseek($this->fh, $this->getLong3());
  56. $area = $this->getInfo(); break;
  57. case 2: //地区被重定向
  58. fseek($this->fh, $this->getLong3());
  59. $area = $this->getInfo(); break;
  60. default: $area = $this->getInfo($byte); break; //地区没有被重定向
  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. //二分查找
  70. $l = 0;
  71. $r = $this->total;
  72. while($l <= $r) {
  73. $m = floor(($l + $r) / 2); //计算中间索引
  74. fseek($this->fh, $this->first + $m * 7);
  75. $beginip = strrev(fread($this->fh, 4)); //中间索引的开始IP地址
  76. fseek($this->fh, $this->getLong3());
  77. $endip = strrev(fread($this->fh, 4)); //中间索引的结束IP地址
  78. if ($ip < $beginip) { //用户的IP小于中间索引的开始IP地址时
  79. $r = $m - 1;
  80. } else {
  81. if ($ip > $endip) { //用户的IP大于中间索引的结束IP地址时
  82. $l = $m + 1;
  83. } else { //用户IP在中间索引的IP范围内时
  84. $findip = $this->first + $m * 7;
  85. break;
  86. }
  87. }
  88. }
  89. //查询国家地区信息
  90. fseek($this->fh, $findip);
  91. $location['beginip'] = long2ip($this->getLong4()); //用户IP所在范围的开始地址
  92. $offset = $this->getlong3();
  93. fseek($this->fh, $offset);
  94. $location['endip'] = long2ip($this->getLong4()); //用户IP所在范围的结束地址
  95. $byte = fread($this->fh, 1); //标志字节
  96. switch (ord($byte)) {
  97. case 1: //国家和区域信息都被重定向
  98. $countryOffset = $this->getLong3(); //重定向地址
  99. fseek($this->fh, $countryOffset);
  100. $byte = fread($this->fh, 1); //标志字节
  101. switch (ord($byte)) {
  102. case 2: //国家信息被二次重定向
  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: //国家信息没有被二次重定向
  109. $location['country'] = $this->getInfo($byte);
  110. $location['area'] = $this->getArea();
  111. break;
  112. }
  113. break;
  114. case 2: //国家信息被重定向
  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: //国家信息没有被重定向
  121. $location['country'] = $this->getInfo($byte);
  122. $location['area'] = $this->getArea();
  123. break;
  124. }
  125. //gb2312 to utf-8(去除无信息时显示的CZ88.NET)
  126. foreach ($location as $k => $v) {
  127. $location[$k] = str_replace('CZ88.NET','',iconv('gb2312', 'utf-8', $v));
  128. }
  129. return $location;
  130. }
  131. //析构函数
  132. function __destruct() {
  133. fclose($this->fh);
  134. }
  135. }
  136. $ip = new ip();
  137. $addr = $ip -> ip2addr('IP地址');
  138. print_r($addr);
  139. ?>

人气教程排行