加入收藏 | 设为首页 | 会员中心 | 我要投稿 大连站长网 (https://www.0411zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

解析php依据ip查询所在地区

发布时间:2022-07-20 22:08:47 所属栏目:PHP教程 来源:互联网
导读:dat文件,关于ip对应地区的信息文件 qqwry.dat文件 网上自己下载 class类文件,解析qqwry.data文件的 IpLocation.php文件 复制代码 代码如下: ?php class IpLocation { /** * @var resource 指针 */ private $fp; /** * 第一条IP记录的偏移地址 * @var int
  dat文件,关于ip对应地区的信息文件
 
  qqwry.dat文件
 
  网上自己下载
 
  class类文件,解析qqwry.data文件的
 
  IpLocation.php文件
 
  复制代码 代码如下:
 
  <?php
 
  class IpLocation {
 
  /**
 
  * @var resource 指针
 
  */
 
  private $fp;
 
  /**
 
  * 第一条IP记录的偏移地址
 
  * @var int
 
  */
 
  private $firstip;
 
  /**
 
  * 最后一条IP记录的偏移地址
 
  * @var int
 
  */
 
  private $lastip;
 
  /**
 
  * IP记录的总条数(不包含版本信息记录)
 
  * @var int
 
  */
 
  private $totalip;
 
  /**
 
  * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
 
  * @param string $filename
 
  * @return IpLocation
 
  */
 
  public function __construct($filename = "qqwry.dat") {
 
  $this->fp = 0;
 
  if (($this->fp = @fopen($filename, 'rb')) !== false) {
 
  $this->firstip = $this->getlong();
 
  $this->lastip = $this->getlong();
 
  $this->totalip = ($this->lastip - $this->firstip) / 7;
 
  }
 
  }
 
  /**
 
  * 返回读取的长整型数
 
  * @access private
 
  * @return int
 
  */
 
  public function getlong() {
 
  //将读取的little-endian编码的4个字节转化为长整型数
 
  $result = unpack('Vlong', fread($this->fp, 4));
 
  return $result['long'];
 
  //获取查找到的IP地理位置信息
 
  fseek($this->fp, $findip);
 
  $location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址
 
  $offset = $this->getlong3();
 
  fseek($this->fp, $offset);
 
  $location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址
 
  $byte = fread($this->fp, 1); // 标志字节
 
  switch (ord($byte)) {
 
  case 1: // 标志字节为1,表示国家和区域信息都被同时重定向
 
  $countryOffset = $this->getlong3(); // 重定向地址
 
  fseek($this->fp, $countryOffset);
 
  $byte = fread($this->fp, 1); // 标志字节
 
  switch (ord($byte)) {
 
  case 2: // 标志字节为2,表示国家信息又被重定向
 
  fseek($this->fp, $this->getlong3());
 
  $location['country'] = $this->getstring();
 
  fseek($this->fp, $countryOffset + 4);
 
  $location['area'] = $this->getarea();
 
  break;
 
  default: // 否则,表示国家信息没有被重定向
 
  $location['country'] = $this->getstring($byte);
 
  $location['area'] = $this->getarea();
 
  break;
 
  }
 
  break;
 
  case 2: // 标志字节为2,表示国家信息被重定向
 
  fseek($this->fp, $this->getlong3());
 
  $location['country'] = $this->getstring();
 
  fseek($this->fp, $offset + 8);
 
  $location['area'] = $this->getarea();
 
  break;
 
  default: // 否则,表示国家信息没有被重定向
 
  $location['country'] = $this->getstring($byte);
 
  $location['area'] = $this->getarea();
 
  break;
 
  }
 
  if ($location['country'] == " CZ88.NET") { // CZ88.NET表示没有有效信息
 
  $location['country'] = "未知";
 
  }
 
  if ($location['area'] == " CZ88.NET") {
 
  $location['area'] = "";
 
  }
 
  return $location;
 
  }
 
  /**
 
  * 析构函数,用于在页面执行结束后自动关闭打开的文件。
 
  *
 
  */
 
  function __desctruct() {
 
  if ($this->fp) {
 
  fclose($this->fp);
 
  }
 
  $this->fp = 0;
 
  }
 
  }
 
  ?>
 
  这个也可以网上下载,也可以copy这里的,这里的也是很全的。
 

(编辑:大连站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!