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 相关文章推荐
WINDOWS下php5.2.4+mysql6.0+apache2.2.4+ZendOptimizer-3.3.0配置
Mar 28 PHP
linux php mysql数据库备份实现代码
Mar 10 PHP
PHP Array交叉表实现代码
Aug 05 PHP
php批量删除数据库下指定前缀的表以prefix_为例
Aug 24 PHP
Yii框架在页面输出执行sql语句以方便调试的实现方法
Dec 24 PHP
PHP访问Google Search API的方法
Mar 05 PHP
PHP数据库连接mysql与mysqli对比分析
Jan 04 PHP
Symfony2之session与cookie用法小结
Mar 18 PHP
php中Redis的应用--消息传递
Mar 28 PHP
学习thinkphp5.0验证类使用方法
Nov 16 PHP
PHP安装BCMath扩展的方法
Feb 13 PHP
php 使用 __call实现重载功能示例
Nov 18 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 读取和修改大文件的某行内容的代码
2009/10/30 PHP
PHP CodeBase:将时间显示为&quot;刚刚&quot;&quot;n分钟/小时前&quot;的方法详解
2013/06/06 PHP
php把大写命名转换成下划线分割命名
2015/04/27 PHP
thinkphp5 migrate数据库迁移工具
2018/02/20 PHP
PHP实现的mysql读写分离操作示例
2018/05/22 PHP
Laravel 5.5 实现禁用用户注册示例
2019/10/24 PHP
js实现连续英文字符自动换行兼容ie6 ie7和firefox
2013/09/06 Javascript
NodeJS url验证(url-valid)的使用方法
2013/11/18 NodeJs
简介JavaScript中setUTCSeconds()方法的使用
2015/06/12 Javascript
第一次接触神奇的Bootstrap导航条
2016/08/09 Javascript
AngularJS中指令的四种基本形式实例分析
2016/11/22 Javascript
jQuery实现扑克正反面翻牌效果
2017/03/10 Javascript
JS实现简单的天数计算器完整实例
2017/04/28 Javascript
浅谈vue中组件绑定事件时是否加.native
2019/11/09 Javascript
Python基于smtplib实现异步发送邮件服务
2015/05/28 Python
Python实现带下标索引的遍历操作示例
2019/05/30 Python
简单了解django orm中介模型
2019/07/30 Python
win10下python2和python3共存问题解决方法
2019/12/23 Python
导入tensorflow:ImportError: libcublas.so.9.0 报错
2020/01/06 Python
MxNet预训练模型到Pytorch模型的转换方式
2020/05/25 Python
python日志通过不同的等级打印不同的颜色(示例代码)
2021/01/13 Python
美国女士内衣在线折扣商店:One Hanes Place
2019/03/24 全球购物
什么是serialVersionUID
2016/03/04 面试题
应届电子商务毕业自荐书范文
2014/02/11 职场文书
超市商业计划书
2014/05/04 职场文书
运动会的口号
2014/06/09 职场文书
数学教育专业求职信
2014/07/22 职场文书
2014个人四风对照检查材料思想汇报
2014/09/18 职场文书
2014年银行员工工作总结
2014/11/12 职场文书
合同权益转让协议书模板
2014/11/18 职场文书
银行反洗钱宣传活动总结
2015/05/08 职场文书
交流会主持词
2015/07/02 职场文书
红十字会救护培训简讯
2015/07/20 职场文书
python中的None与NULL用法说明
2021/05/25 Python
MySQL的prepare使用以及遇到的bug
2022/05/11 MySQL
python 使用pandas读取csv文件的方法
2022/12/24 Python