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 相关文章推荐
一个分页的论坛
Oct 09 PHP
php adodb分页实现代码
Mar 19 PHP
php preg_filter执行一个正则表达式搜索和替换
Feb 27 PHP
php利用cookie实现自动登录的方法
Dec 10 PHP
详解Grunt插件之LiveReload实现页面自动刷新(两种方案)
Jul 31 PHP
实例讲解yii2.0在php命令行中运行的步骤
Dec 01 PHP
Symfony2使用Doctrine进行数据库查询方法实例总结
Mar 18 PHP
PHP使用stream_context_create()模拟POST/GET请求的方法
Apr 02 PHP
php微信浏览器分享设置以及回调详解
Aug 01 PHP
php实现微信扫码支付
Mar 26 PHP
php-beanstalkd消息队列类实例分享
Jul 19 PHP
WordPress免插件实现面包屑导航的示例代码
Aug 20 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
咖啡知识大全
2021/03/03 新手入门
php内存缓存实现方法
2015/01/24 PHP
让你的PHP7更快之Hugepage用法分析
2016/05/31 PHP
Laravel Eloquent ORM 多条件查询的例子
2019/10/10 PHP
laravel7学习之无限级分类的最新实现方法
2020/09/30 PHP
如何用javascript去掉字符串里的所有空格
2007/02/08 Javascript
jQuery功能函数详解
2015/02/01 Javascript
javaScript的函数对象的声明详解
2015/02/06 Javascript
js过滤HTML标签完整实例
2015/11/26 Javascript
使用jQuery判断浏览器滚动条位置的方法
2016/05/30 Javascript
利用Node.js了解与测量HTTP所花费的时间详解
2017/09/22 Javascript
jQuery实现表格隔行换色
2018/09/01 jQuery
详解Node.js amqplib 连接 Rabbit MQ最佳实践
2019/01/24 Javascript
利用Webpack实现小程序多项目管理的方法
2019/02/25 Javascript
使用layui日期控件laydate对开始和结束时间进行联动控制的方法
2019/09/06 Javascript
jquery 键盘事件 keypress() keydown() keyup()用法总结
2019/10/23 jQuery
jQuery实现轮播图效果demo
2020/01/11 jQuery
Angular 多模块项目构建过程
2020/02/13 Javascript
详细分析Node.js 多进程
2020/06/22 Javascript
vue.js click点击事件获取当前元素对象的操作
2020/08/07 Javascript
vue+高德地图实现地图搜索及点击定位操作
2020/09/09 Javascript
JavaScript 实现下雪特效的示例代码
2020/09/09 Javascript
Python计算程序运行时间的方法
2014/12/13 Python
python脚本设置超时机制系统时间的方法
2016/02/21 Python
浅谈python类属性的访问、设置和删除方法
2016/07/25 Python
python判断一个数是否能被另一个整数整除的实例
2018/12/12 Python
python2和python3实现在图片上加汉字的方法
2019/08/22 Python
Python使用正则表达式实现爬虫数据抽取
2020/08/17 Python
HTML5 transform三维立方体实现360无死角三维旋转效果
2014/08/22 HTML / CSS
2014两会优秀的心得体会范文
2014/03/17 职场文书
公司演讲稿开场白
2014/08/25 职场文书
领导班子三严三实心得体会
2014/10/13 职场文书
七一活动主持词
2015/06/29 职场文书
2019年个人工作总结范文
2019/03/25 职场文书
Golang实现AES对称加密的过程详解
2021/05/20 Golang
Python时间操作之pytz模块使用详解
2022/06/14 Python