打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PHP 直接从 QQWry.dat 解析IP地址的程序
  1. <?php
  2. #文件名:QQWry.php
  3. /*++++++++++++++++++++++++++++++++++++
  4. 程序名称:IP解析程序
  5. 程序功能:基于QQ的二进制数据库QQWry.Dat
  6. 程序作者:未知
  7. 使用方法:
  8.  
  9. 请将文件 QQWry.Dat 置于当前目录中
  10.  
  11. #实例+++++++++++++++++++++++++++++++
  12. $ip="202.201.48.1";#
  13. $QQWry=new QQWry;
  14. $ifErr=$QQWry->QQWry($ip);
  15. echo "$QQWry->Country$QQWry->Local";
  16.  
  17. +++++++++++++++++++++++++++++++++++++
  18.  
  19. ip.php是测试程序
  20. ++++++++++++++++++++++++++++++++++
  21.  
  22. 欢迎访问:[url]http://www.zh5j.com[/url]
  23. +++++++++++++++++++++++++++++++++++++*/
  24.  
  25. define(__QQWRY__ , dirname(__FILE__)."/QQWry.Dat");
  26.  
  27. //echo __QQWRY__;
  28. class QQWry{
  29.     var $StartIP=0;
  30.     var $EndIP=0;
  31.     var $Country=‘‘;
  32.     var $Local=‘‘;
  33.  
  34.     var $CountryFlag=0; // 标识 Country位置
  35.              // 0x01,随后3字节为Country偏移,没有Local
  36.              // 0x02,随后3字节为Country偏移,接着是Local
  37.              // 其他,Country,Local,Local有类似的压缩。可能多重引用。
  38.     var $fp;
  39.  
  40.     var $FirstStartIp=0;
  41.     var $LastStartIp=0;
  42.     var $EndIpOff=0 ;
  43.  
  44.     function getStartIp($RecNo){
  45.      $offset=$this->FirstStartIp+$RecNo * 7 ;
  46.      @fseek($this->fp,$offset,SEEK_SET) ;
  47.      $buf=fread($this->fp ,7) ;
  48.      $this->EndIpOff=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])* 256*256);
  49.      $this->StartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
  50.      return $this->StartIp;
  51.     }
  52.  
  53.     function getEndIp(){
  54.      @fseek ( $this->fp , $this->EndIpOff , SEEK_SET ) ;
  55.      $buf=fread ( $this->fp , 5 ) ;
  56.      $this->EndIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
  57.      $this->CountryFlag=ord ( $buf[4] ) ;
  58.      return $this->EndIp ;
  59.     }
  60.  
  61.     function getCountry(){
  62.      switch ( $this->CountryFlag ) {
  63.         case 1:
  64.         case 2:
  65.          $this->Country=$this->getFlagStr ( $this->EndIpOff+4) ;
  66.          //echo sprintf(‘EndIpOffset=(%x)‘,$this->EndIpOff );
  67.          $this->Local=( 1 == $this->CountryFlag )? ‘‘ : $this->getFlagStr ( $this->EndIpOff+8);
  68.          break ;
  69.         default :
  70.          $this->Country=$this->getFlagStr ($this->EndIpOff+4) ;
  71.          $this->Local=$this->getFlagStr ( ftell ( $this->fp )) ;
  72.      }
  73.     }
  74.  
  75.  
  76.     function getFlagStr ($offset){
  77.      $flag=0 ;
  78.      while(1){
  79.         @fseek($this->fp ,$offset,SEEK_SET) ;
  80.         $flag=ord(fgetc($this->fp ) ) ;
  81.         if ( $flag == 1 || $flag == 2 ) {
  82.          $buf=fread ($this->fp , 3 ) ;
  83.          if ($flag==2){
  84.             $this->CountryFlag=2;
  85.             $this->EndIpOff=$offset - 4 ;
  86.          }
  87.          $offset=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256);
  88.         }
  89.         else{
  90.          break ;
  91.         }
  92.  
  93.      }
  94.      if($offset<12)
  95.         return ‘‘;
  96.      @fseek($this->fp , $offset , SEEK_SET ) ;
  97.  
  98.      return $this->getStr();
  99.     }
  100.  
  101.     function getStr ( )
  102.     {
  103.      $str=‘‘ ;
  104.      while ( 1 ) {
  105.         $c=fgetc ( $this->fp ) ;
  106.         //echo "$cn" ;
  107.  
  108.         if(ord($c[0])== 0 )
  109.          break ;
  110.         $str.= $c ;
  111.      }
  112.      //echo "$str n";
  113.      return $str ;
  114.     }
  115.  
  116.  
  117.     function qqwry ($dotip=‘‘) {
  118.         if(!$dotip)return;
  119.             if(ereg("^(127)",$dotip)){$this->Country=本地网络;return;}
  120.             elseif(ereg("^(192)",$dotip)) {$this->Country=局域网;return;}
  121.  
  122.      $nRet;
  123.      $ip=$this->IpToInt ( $dotip );
  124.      $this->fp= fopen(__QQWRY__, "rb");
  125.      if ($this->fp == NULL) {
  126.          $szLocal= "OpenFileError";
  127.         return 1;
  128.  
  129.      }
  130.      @fseek ( $this->fp , 0 , SEEK_SET ) ;
  131.      $buf=fread ( $this->fp , 8 ) ;
  132.      $this->FirstStartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
  133.      $this->LastStartIp=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])*256*256) + (ord($buf[7])*256*256*256);
  134.  
  135.      $RecordCount= floor( ( $this->LastStartIp - $this->FirstStartIp ) / 7);
  136.      if ($RecordCount <= 1){
  137.         $this->Country="FileDataError";
  138.         fclose($this->fp) ;
  139.         return 2 ;
  140.      }
  141.  
  142.      $RangB= 0;
  143.      $RangE= $RecordCount;
  144.      // Match ...
  145.      while ($RangB < $RangE-1)
  146.      {
  147.      $RecNo= floor(($RangB + $RangE) / 2);
  148.      $this->getStartIp ( $RecNo ) ;
  149.  
  150.         if ( $ip == $this->StartIp )
  151.         {
  152.          $RangB=$RecNo ;
  153.          break ;
  154.         }
  155.      if ($ip>$this->StartIp)
  156.         $RangB= $RecNo;
  157.      else
  158.         $RangE= $RecNo;
  159.      }
  160.      $this->getStartIp ( $RangB ) ;
  161.      $this->getEndIp ( ) ;
  162.  
  163.      if ( ( $this->StartIp <= $ip ) && ( $this->EndIp >= $ip ) ){
  164.         $nRet=0 ;
  165.         $this->getCountry ( ) ;
  166.         //这样不太好..............所以..........
  167.         $this->Local=str_replace("(我们一定要解放台湾!!!)", "", $this->Local);
  168.      }
  169.      else{
  170.         $nRet=3 ;
  171.         $this->Country=未知 ;
  172.         $this->Local=‘‘ ;
  173.      }
  174.      fclose ( $this->fp );
  175. $this->Country=preg_replace("/(CZ88.NET)|(纯真网络)/","21andy.com",$this->Country);
  176. $this->Local=preg_replace("/(CZ88.NET)|(纯真网络)/","21andy.com",$this->Local);
  177. //////////////看看 $nRet在上面的值是什么0和3,于是将下面的行注释掉
  178.         return $nRet ;
  179.  
  180.      //return "$this->Country $this->Local";#如此直接返回位置和国家便可以了
  181.     }
  182.  
  183.     function IpToInt($Ip) {
  184.      $array=explode(.,$Ip);
  185.      $Int=($array[0] * 256*256*256) + ($array[1]*256*256) + ($array[2]*256) + $array[3];
  186.      return $Int;
  187.     }
  188. }
  189. function GetIP(){//获取IP
  190.     return $_SERVER[REMOTE_ADDR]?$_SERVER[REMOTE_ADDR]:$GLOBALS[HTTP_SERVER_VARS][REMOTE_ADDR];
  191. }
  192. ?>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
用VBS脚本查询纯真IP库QQWry.dat(转)
操作qqwary.dat数据文件的类
ShopNC 商城系统开发经验分享第八篇 二次开发之分地区开发
纯真IP数据库格式详解
【转载】 国外ip数据库
纯真IP地址数据库qqwry.dat解析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服