php读取纯真ip数据库使用示例


Posted in PHP onJanuary 26, 2014
<?php
/*--------------------------------------------------
 ip2address [qqwry.dat]
--------------------------------------------------*/
class ip {
 var $fh; //IP数据库文件句柄
 var $first; //第一条索引
 var $last; //最后一条索引
 var $total; //索引总数
 //构造函数
 function __construct() {
  $this->fh = fopen('qqwry.dat', 'rb'); //qqwry.dat文件
  $this->first = $this->getLong4();
  $this->last = $this->getLong4();
  $this->total = ($this->last - $this->first) / 7; //每条索引7字节
 }
 //检查IP合法性
 function checkIp($ip) {
  $arr = explode('.',$ip);
  if(count($arr) !=4 ) {
   return false;
  } else {
   for ($i=0; $i < 4; $i++) {
    if ($arr[$i] <'0' || $arr[$i] > '255') {
     return false;
    }
   }
  }
  return true;
 }
 function getLong4() {
  //读取little-endian编码的4个字节转化为长整型数
  $result = unpack('Vlong', fread($this->fh, 4));
  return $result['long'];
 }
 function getLong3() {
  //读取little-endian编码的3个字节转化为长整型数
  $result = unpack('Vlong', fread($this->fh, 3).chr(0));
  return $result['long'];
 }
 //查询信息
 function getInfo($data = "") {
  $char = fread($this->fh, 1);
  while (ord($char) != 0) { //国家地区信息以0结束
   $data .= $char;
   $char = fread($this->fh, 1);
  }
  return $data;
 }
 //查询地区信息
 function getArea() {
  $byte = fread($this->fh, 1); //标志字节
  switch (ord($byte)) {
   case 0: $area = ''; break; //没有地区信息
   case 1: //地区被重定向
    fseek($this->fh, $this->getLong3());
    $area = $this->getInfo(); break;
   case 2: //地区被重定向
   fseek($this->fh, $this->getLong3());
   $area = $this->getInfo(); break;
   default: $area = $this->getInfo($byte);  break; //地区没有被重定向
  }
  return $area;
 }
 function ip2addr($ip) {
  if(!$this -> checkIp($ip)){
   return false;
  }
  $ip = pack('N', intval(ip2long($ip)));
  //二分查找
  $l = 0;
  $r = $this->total;
  while($l <= $r) {
   $m = floor(($l + $r) / 2); //计算中间索引
   fseek($this->fh, $this->first + $m * 7);
   $beginip = strrev(fread($this->fh, 4)); //中间索引的开始IP地址
   fseek($this->fh, $this->getLong3());
   $endip = strrev(fread($this->fh, 4)); //中间索引的结束IP地址
   if ($ip < $beginip) { //用户的IP小于中间索引的开始IP地址时
    $r = $m - 1;
   } else {
    if ($ip > $endip) { //用户的IP大于中间索引的结束IP地址时
     $l = $m + 1;
    } else { //用户IP在中间索引的IP范围内时
     $findip = $this->first + $m * 7;
     break;
    }
   }
  }
  //查询国家地区信息
  fseek($this->fh, $findip);
  $location['beginip'] = long2ip($this->getLong4()); //用户IP所在范围的开始地址
  $offset = $this->getlong3();
  fseek($this->fh, $offset);
  $location['endip'] = long2ip($this->getLong4()); //用户IP所在范围的结束地址
  $byte = fread($this->fh, 1); //标志字节
  switch (ord($byte)) {
   case 1:  //国家和区域信息都被重定向
    $countryOffset = $this->getLong3(); //重定向地址
    fseek($this->fh, $countryOffset);
    $byte = fread($this->fh, 1); //标志字节
    switch (ord($byte)) {
     case 2: //国家信息被二次重定向
      fseek($this->fh, $this->getLong3());
      $location['country'] = $this->getInfo();
      fseek($this->fh, $countryOffset + 4);
      $location['area'] = $this->getArea();
      break;
     default: //国家信息没有被二次重定向
      $location['country'] = $this->getInfo($byte);
      $location['area'] = $this->getArea();
      break;
    }
    break;
   case 2: //国家信息被重定向
    fseek($this->fh, $this->getLong3());
    $location['country'] = $this->getInfo();
    fseek($this->fh, $offset + 8);
    $location['area'] = $this->getArea();
    break;
   default: //国家信息没有被重定向
    $location['country'] = $this->getInfo($byte);
    $location['area'] = $this->getArea();
    break;
  }
  //gb2312 to utf-8(去除无信息时显示的CZ88.NET)
  foreach ($location as $k => $v) {
   $location[$k] = str_replace('CZ88.NET','',iconv('gb2312', 'utf-8', $v));
  }
  return $location;
 }
 //析构函数
 function __destruct() {
  fclose($this->fh);
 }
}
$ip = new ip();
$addr = $ip -> ip2addr('IP地址');
print_r($addr);
?>
PHP 相关文章推荐
PHP 5.0 Pear安装方法
Dec 06 PHP
php代码把全角数字转为半角数字
Dec 10 PHP
用PHP实现递归循环每一个目录
Aug 08 PHP
php并发对MYSQL造成压力的解决方法
Feb 21 PHP
PHP array_key_exists检查键名或索引是否存在于数组中的实现方法
Jun 13 PHP
PHP实现添加购物车功能
Mar 06 PHP
PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能实例
May 17 PHP
PHP实现Redis单据锁以及防止并发重复写入
Apr 10 PHP
PHP扩展Swoole实现实时异步任务队列示例
Apr 13 PHP
基于ThinkPHP5框架使用QueryList爬取并存入mysql数据库操作示例
May 25 PHP
PHP常用正则表达式精选(推荐)
May 28 PHP
php+iframe 实现上传文件功能示例
Mar 04 PHP
curl不使用文件存取cookie php使用curl获取cookie示例
Jan 26 #PHP
php版小黄鸡simsimi聊天机器人接口分享
Jan 26 #PHP
百度ping方法使用示例 自动ping百度
Jan 26 #PHP
PHP弹出提示框并跳转到新页面即重定向到新页面
Jan 24 #PHP
header导出Excel应用示例
Jan 24 #PHP
使用openssl实现rsa非对称加密算法示例
Jan 24 #PHP
测试php连接mysql是否成功的代码分享
Jan 24 #PHP
You might like
php中DOMElement操作xml文档实例演示
2013/03/26 PHP
PHP结合Jquery和ajax实现瀑布流特效
2016/01/07 PHP
关于PHP文件的自动运行方法分析
2016/05/13 PHP
PHP生成短网址的思路以及实现方法的详解
2019/03/25 PHP
键盘上一张下一张兼容IE/google/firefox等浏览器
2014/01/28 Javascript
Bootstrap整体框架之CSS12栅格系统
2016/12/15 Javascript
jQuery Validate 数组 全部验证问题
2017/01/12 Javascript
js中常用的Math方法总结
2017/01/12 Javascript
移动端利用H5实现压缩图片上传功能
2017/03/29 Javascript
详解微信小程序Radio选中样式切换
2017/07/06 Javascript
详解easyui 切换主题皮肤
2019/04/04 Javascript
python实现在字符串中查找子字符串的方法
2015/07/11 Python
全面理解Python中self的用法
2016/06/04 Python
python生成式的send()方法(详解)
2017/05/08 Python
Python3爬虫学习之应对网站反爬虫机制的方法分析
2018/12/12 Python
Python+OpenCV图片局部区域像素值处理改进版详解
2019/01/23 Python
python实现贪吃蛇小游戏
2020/03/21 Python
解决python 3 urllib 没有 urlencode 属性的问题
2019/08/22 Python
利用python中集合的唯一性实现去重
2020/02/11 Python
PageFactory设计模式基于python实现
2020/04/14 Python
使用keras和tensorflow保存为可部署的pb格式
2020/05/25 Python
Python使用Selenium实现淘宝抢单的流程分析
2020/06/23 Python
HTML5 拖放(Drag 和 Drop)详解与实例代码
2017/09/14 HTML / CSS
英国著名国际平价时尚男装品牌:Topman
2016/08/27 全球购物
zooplus德国:便宜地订购动物用品、动物饲料、动物食品
2020/05/06 全球购物
幼儿园家长评语
2014/02/10 职场文书
经典商业广告词
2014/03/13 职场文书
课程改革实施方案
2014/03/16 职场文书
珠宝的促销活动方案
2014/08/31 职场文书
纪念9.18事变演讲稿
2014/09/14 职场文书
党员先进性教育整改措施
2014/09/18 职场文书
学校纪律作风整改措施思想汇报
2014/10/11 职场文书
环卫个人总结
2015/03/03 职场文书
创业计划书之电动车企业
2019/10/11 职场文书
SQL Server Agent 服务无法启动
2022/04/20 SQL Server
html,css,javascript是怎样变成页面的
2023/05/07 HTML / CSS