[PHP] ThinkPHP IP地理位置查询类(附IP地址数据库UTFWry.dat) →→→→→进入此内容的聊天室

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

回复 "ThinkPHP IP地理位置查询类(附IP地址数据库UTFWry.dat)"

这儿你可以回复上面这条便签

captcha