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

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

回复 "IP地理位置查询类(IP地址数据库)"

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

captcha