欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-02-12 18:29:58
...
  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. ?>
复制代码