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 相关文章推荐
从Web查询数据库之PHP与MySQL篇
Sep 25 PHP
《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
Nov 01 PHP
优化php效率,提高php性能的一些方法
Mar 24 PHP
php获取汉字拼音首字母的方法
Oct 21 PHP
PHP 搜索查询功能实现
Nov 29 PHP
php微信公众平台开发(三)订阅事件处理
Dec 06 PHP
PHP 序列化和反序列化函数实例详解
Jul 18 PHP
Yii输入正确验证码却验证失败的解决方法
Jun 06 PHP
PHP实现求两个字符串最长公共子串的方法示例
Nov 17 PHP
PHP面向对象程序设计之接口的继承定义与用法详解
Dec 20 PHP
laravel框架模板之公共模板、继承、包含实现方法分析
Aug 30 PHP
laravel 模型查询按照whereIn排序的示例
Oct 16 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
摘自织梦CMS中的图片处理类
2015/08/08 PHP
PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
2019/12/20 PHP
任意位置显示html菜单
2007/02/01 Javascript
用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
2012/06/22 Javascript
ECMAScript 创建自己的js类库
2012/11/22 Javascript
Javascript中引用示例介绍
2014/02/21 Javascript
JS替换字符串中空格方法
2015/04/17 Javascript
JS拖动鼠标画出方框实现鼠标选区的方法
2015/08/05 Javascript
jquery实现点击展开列表同时隐藏其他列表
2015/08/10 Javascript
JavaScript File API文件上传预览
2016/02/02 Javascript
javascript与jquery动态创建html元素示例
2016/07/25 Javascript
微信小程序 获取微信OpenId详解及实例代码
2016/10/31 Javascript
javascript基础进阶_深入剖析执行环境及作用域链
2017/09/05 Javascript
Vue自定义toast组件的实例代码
2018/08/15 Javascript
解决node-sass偶尔安装失败的方法小结
2018/12/05 Javascript
原生JS 实现的input输入时表格过滤操作示例
2019/08/03 Javascript
理解Python中的类与实例
2015/04/27 Python
Python简单格式化时间的方法【strftime函数】
2016/09/18 Python
Django Web开发中django-debug-toolbar的配置以及使用
2018/05/06 Python
对numpy数据写入文件的方法讲解
2018/07/09 Python
python 含子图的gif生成时内存溢出的方法
2019/07/07 Python
Django-Scrapy生成后端json接口的方法示例
2020/10/06 Python
css3的图形3d翻转效果应用示例
2014/04/08 HTML / CSS
新加坡最受追捧的体验平台:Hapz
2018/01/01 全球购物
ManoMano英国:欧洲第一家专注于DIY和园艺市场的电商平台
2020/03/12 全球购物
视图的作用
2014/12/19 面试题
办公室文秘自我鉴定
2013/09/21 职场文书
国际贸易专业个人求职信格式
2014/02/02 职场文书
小学生环保演讲稿
2014/04/25 职场文书
沈阳故宫导游词
2015/01/31 职场文书
总账会计岗位职责
2015/04/02 职场文书
导游词之上饶龟峰
2019/10/25 职场文书
小型企业的绩效考核制度模板
2019/11/21 职场文书
python tkinter模块的简单使用
2021/04/07 Python
k8s部署redis cluster集群的实现
2021/06/24 Redis
MySQL 开窗函数
2022/02/15 MySQL