IpLocation.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace network;
  3. /**
  4. * IP 地理位置查询类 修改自 CoolCode.CN
  5. * 由于使用UTF8编码 如果使用纯真IP地址库的话 需要对返回结果进行编码转换
  6. * @category ORG
  7. * @package ORG
  8. * @subpackage Net
  9. * @author liu21st <liu21st@gmail.com>
  10. */
  11. class IpLocation
  12. {
  13. /**
  14. * QQWry.Dat文件指针
  15. *
  16. * @var resource
  17. */
  18. private $fp;
  19. /**
  20. * 第一条IP记录的偏移地址
  21. *
  22. * @var int
  23. */
  24. private $firstip;
  25. /**
  26. * 最后一条IP记录的偏移地址
  27. *
  28. * @var int
  29. */
  30. private $lastip;
  31. /**
  32. * IP记录的总条数(不包含版本信息记录)
  33. *
  34. * @var int
  35. */
  36. private $totalip;
  37. /**
  38. * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
  39. *
  40. * @param string $filename
  41. * @return IpLocation
  42. */
  43. public function __construct($filename = "UTFWry.dat")
  44. {
  45. $this->fp = 0;
  46. if (($this->fp = fopen(dirname(__FILE__) . '/' . $filename, 'rb')) !== false) {
  47. $this->firstip = $this->getlong();
  48. $this->lastip = $this->getlong();
  49. $this->totalip = ($this->lastip - $this->firstip) / 7;
  50. }
  51. }
  52. /**
  53. * 返回读取的长整型数
  54. *
  55. * @access private
  56. * @return int
  57. */
  58. private function getlong()
  59. {
  60. //将读取的little-endian编码的4个字节转化为长整型数
  61. $result = unpack('Vlong', fread($this->fp, 4));
  62. return $result['long'];
  63. }
  64. /**
  65. * 返回读取的3个字节的长整型数
  66. *
  67. * @access private
  68. * @return int
  69. */
  70. private function getlong3()
  71. {
  72. //将读取的little-endian编码的3个字节转化为长整型数
  73. $result = unpack('Vlong', fread($this->fp, 3) . chr(0));
  74. return $result['long'];
  75. }
  76. /**
  77. * 返回压缩后可进行比较的IP地址
  78. *
  79. * @access private
  80. * @param string $ip
  81. * @return string
  82. */
  83. private function packip($ip)
  84. {
  85. // 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False,
  86. // 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串
  87. return pack('N', intval(ip2long($ip)));
  88. }
  89. /**
  90. * 返回读取的字符串
  91. *
  92. * @access private
  93. * @param string $data
  94. * @return string
  95. */
  96. private function getstring($data = "")
  97. {
  98. $char = fread($this->fp, 1);
  99. while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束
  100. $data .= $char; // 将读取的字符连接到给定字符串之后
  101. $char = fread($this->fp, 1);
  102. }
  103. return $data;
  104. }
  105. /**
  106. * 返回地区信息
  107. *
  108. * @access private
  109. * @return string
  110. */
  111. private function getarea()
  112. {
  113. $byte = fread($this->fp, 1); // 标志字节
  114. switch (ord($byte)) {
  115. case 0: // 没有区域信息
  116. $area = "";
  117. break;
  118. case 1:
  119. case 2: // 标志字节为1或2,表示区域信息被重定向
  120. fseek($this->fp, $this->getlong3());
  121. $area = $this->getstring();
  122. break;
  123. default: // 否则,表示区域信息没有被重定向
  124. $area = $this->getstring($byte);
  125. break;
  126. }
  127. return $area;
  128. }
  129. /**
  130. * 根据所给 IP 地址或域名返回所在地区信息
  131. *
  132. * @access public
  133. * @param string $ip
  134. * @return array
  135. */
  136. public function getlocation($ip = '')
  137. {
  138. if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空
  139. if (empty($ip)) $ip = get_client_ip();
  140. $location['ip'] = gethostbyname($ip); // 将输入的域名转化为IP地址
  141. $ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址
  142. // 不合法的IP地址会被转化为255.255.255.255
  143. // 对分搜索
  144. $l = 0; // 搜索的下边界
  145. $u = $this->totalip; // 搜索的上边界
  146. $findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息)
  147. while ($l <= $u) { // 当上边界小于下边界时,查找失败
  148. $i = floor(($l + $u) / 2); // 计算近似中间记录
  149. fseek($this->fp, $this->firstip + $i * 7);
  150. $beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址
  151. // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式
  152. // 以便用于比较,后面相同。
  153. if ($ip < $beginip) { // 用户的IP小于中间记录的开始IP地址时
  154. $u = $i - 1; // 将搜索的上边界修改为中间记录减一
  155. } else {
  156. fseek($this->fp, $this->getlong3());
  157. $endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址
  158. if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时
  159. $l = $i + 1; // 将搜索的下边界修改为中间记录加一
  160. } else { // 用户的IP在中间记录的IP范围内时
  161. $findip = $this->firstip + $i * 7;
  162. break; // 则表示找到结果,退出循环
  163. }
  164. }
  165. }
  166. //获取查找到的IP地理位置信息
  167. fseek($this->fp, $findip);
  168. $location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址
  169. $offset = $this->getlong3();
  170. fseek($this->fp, $offset);
  171. $location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址
  172. $byte = fread($this->fp, 1); // 标志字节
  173. switch (ord($byte)) {
  174. case 1: // 标志字节为1,表示国家和区域信息都被同时重定向
  175. $countryOffset = $this->getlong3(); // 重定向地址
  176. fseek($this->fp, $countryOffset);
  177. $byte = fread($this->fp, 1); // 标志字节
  178. switch (ord($byte)) {
  179. case 2: // 标志字节为2,表示国家信息又被重定向
  180. fseek($this->fp, $this->getlong3());
  181. $location['country'] = $this->getstring();
  182. fseek($this->fp, $countryOffset + 4);
  183. $location['area'] = $this->getarea();
  184. break;
  185. default: // 否则,表示国家信息没有被重定向
  186. $location['country'] = $this->getstring($byte);
  187. $location['area'] = $this->getarea();
  188. break;
  189. }
  190. break;
  191. case 2: // 标志字节为2,表示国家信息被重定向
  192. fseek($this->fp, $this->getlong3());
  193. $location['country'] = $this->getstring();
  194. fseek($this->fp, $offset + 8);
  195. $location['area'] = $this->getarea();
  196. break;
  197. default: // 否则,表示国家信息没有被重定向
  198. $location['country'] = $this->getstring($byte);
  199. $location['area'] = $this->getarea();
  200. break;
  201. }
  202. if ($location['country'] == " CZ88.NET") { // CZ88.NET表示没有有效信息
  203. $location['country'] = "未知";
  204. }
  205. if ($location['area'] == " CZ88.NET") {
  206. $location['area'] = "";
  207. }
  208. return $location;
  209. }
  210. /**
  211. * 析构函数,用于在页面执行结束后自动关闭打开的文件。
  212. *
  213. */
  214. public function __destruct()
  215. {
  216. if ($this->fp) {
  217. fclose($this->fp);
  218. }
  219. $this->fp = 0;
  220. }
  221. }