PHP判断IP并转跳到相应城市分站的方法


Posted in PHP onMarch 25, 2015

本文实例讲述了PHP判断IP并转跳到相应城市分站的方法。分享给大家供大家参考。具体实现方法如下:

<?php  
class QQWry{  
  var $StartIP=0;  
  var $EndIP=0;  
  var $Country='';  
  var $Local='';  
  var $CountryFlag=0;
// 标识 Country位置  
// 0x01,随后3字节为Country偏移,没有Local  
// 0x02,随后3字节为Country偏移,接着是Local  
// 其他,Country,Local,Local有类似的压缩。可能多重引用。  
  var $fp;  
  var $FirstStartIp=0;  
  var $LastStartIp=0;  
  var $EndIpOff=0 ;  
  function getStartIp($RecNo){  
   $offset=$this->FirstStartIp+$RecNo * 7 ;  
   @fseek($this->fp,$offset,SEEK_SET) ;  
   $buf=fread($this->fp ,7) ;  
   $this->EndIpOff=ord($buf[4])+(ord($buf[5])*256)+(ord($buf[6])*256*256);
   $this->StartIp=ord($buf[0])+(ord($buf[1])*256)+(ord($buf[2])*256*256)+(ord($buf[3])*256*256*256);
   return $this->StartIp;
  }
  function getEndIp(){  
   @fseek ( $this->fp , $this->EndIpOff , SEEK_SET ) ;  
   $buf=fread ( $this->fp , 5 ) ;  
   $this->EndIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
   $this->CountryFlag=ord ( $buf[4] ) ;  
   return $this->EndIp ;  
  }  
  function getCountry(){  
   switch ( $this->CountryFlag ) {  
    case 1:  
    case 2:  
     $this->Country=$this->getFlagStr ( $this->EndIpOff+4) ;  
     //echo sprintf('EndIpOffset=(%x)',$this->EndIpOff );  
     $this->Local=( 1 == $this->CountryFlag )? '' : $this->getFlagStr ( $this->EndIpOff+8);
     break ;  
    default :  
     $this->Country=$this->getFlagStr ($this->EndIpOff+4);  
     $this->Local=$this->getFlagStr ( ftell ( $this->fp ));  
   }  
  }  
  function getFlagStr ($offset){  
   $flag=0 ;  
   while(1){  
    @fseek($this->fp ,$offset,SEEK_SET) ;  
    $flag=ord(fgetc($this->fp ) ) ;  
    if ( $flag == 1 || $flag == 2 ) {  
     $buf=fread ($this->fp , 3 ) ;  
     if ($flag==2){  
      $this->CountryFlag=2;  
      $this->EndIpOff=$offset - 4 ;  
     }  
     $offset=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256);  
    }  
    else{  
     break ;  
    }  
   }  
   if($offset<12)  
    return '';  
   @fseek($this->fp , $offset , SEEK_SET ) ;  
  
   return $this->getStr();  
  }  
  function getStr ( )  
  {  
   $str='' ;  
   while ( 1 ) {  
    $c=fgetc ( $this->fp ) ;  
    //echo "$cn" ;  
    if(ord($c[0])== 0 )  
     break ;  
    $str.= $c ;  
   }  
   //echo "$str n";  
   return $str ;  
  }  
  function qqwry ($dotip='') {  
    if( !is_string($dotip) || $dotip==''){return;}  
    if(preg_match("/^127/",$dotip)){$this->Country="本地网络";return ;}  
    elseif(preg_match("/^192/",$dotip)) {$this->Country="局域网";return ;}  
   $nRet;  
   $ip=$this->IpToInt ( $dotip );  
   $this->fp= fopen(__QQWRY__, "rb");  
   if ($this->fp == NULL) {  
     $szLocal= "OpenFileError";  
    return 1;  
   }
   @fseek ( $this->fp , 0 , SEEK_SET );
   $buf=fread ( $this->fp , 8 );
   $this->FirstStartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
   $this->LastStartIp=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])*256*256) + (ord($buf[7])*256*256*256);
   $RecordCount= floor(($this->LastStartIp - $this->FirstStartIp) / 7);
   if ($RecordCount <= 1){  
    $this->Country="FileDataError";  
    fclose($this->fp) ;  
    return 2 ;  
   }  
   $RangB= 0;  
   $RangE= $RecordCount;  
   // Match ...  
   while ($RangB < $RangE-1)  
   {  
   $RecNo= floor(($RangB + $RangE) / 2);
   $this->getStartIp ( $RecNo ) ;  
    if ( $ip == $this->StartIp )  
    {  
     $RangB=$RecNo ;  
     break ;  
    }  
   if ($ip>$this->StartIp)  
    $RangB= $RecNo;  
   else 
    $RangE= $RecNo;  
   }  
   $this->getStartIp ( $RangB ) ;  
   $this->getEndIp ( ) ;  
   if ( ( $this->StartIp <= $ip ) && ( $this->EndIp >= $ip ) ){  
    $nRet=0 ;  
    $this->getCountry ( ) ;  
    //这样不太好..............所以..........  
    $this->Local=str_replace("area error", "",$this->Local);  
   }  
   else{  
    $nRet=3 ;  
    $this->Country='未知' ;  
    $this->Local='' ;  
   }  
   fclose ( $this->fp );  
$this->Country=preg_replace("/(CZ88.NET)|(纯真网络)/","局域网/未知",$this->Country);
$this->Local=preg_replace("/(CZ88.NET)|(纯真网络)/","局域网/未知",$this->Local);  
//////////////看看 $nRet在上面的值是什么0和3,于是将下面的行注释掉  
    return $nRet ;  
//return "$this->Country $this->Local";#如此直接返回位置和国家便可以了  
  }  
  function IpToInt($Ip) {  
   $array=explode('.',$Ip);  
   $Int=($array[0] * 256*256*256) + ($array[1]*256*256) + ($array[2]*256) + $array[3];
   return $Int;  
  }  
 }  
?>

实例演示如下:

$QQWry=new QQWry;
$QQWry->QQWry("60.31.95.255");
$country = $QQWry->Country;
echo $QQWry->Country.",".$QQWry->Local;

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
php mssql 时间格式问题
Jan 13 PHP
全世界最小的php网页木马一枚 附PHP木马的防范方法
Oct 09 PHP
PHP学习笔记之二 php入门知识
Jan 12 PHP
php 操作调试的方法
Jul 12 PHP
PHP中判断文件存在使用is_file还是file_exists?
Apr 03 PHP
php curl模拟post请求和提交多维数组的示例代码
Nov 19 PHP
PHP下的浮点运算不准的解决方法
Oct 27 PHP
ThinkPHP Where 条件中常用表达式示例(详解)
Mar 31 PHP
PHP用PDO如何封装简单易用的DB类详解
Jul 30 PHP
php使用curl下载指定大小的文件实例代码
Sep 30 PHP
ajax+php实现无刷新验证手机号的实例
Dec 22 PHP
浅谈PHP SHA1withRSA加密生成签名及验签
Mar 18 PHP
PHP使用in_array函数检查数组中是否存在某个值
Mar 25 #PHP
PHP+MYSQL实现用户的增删改查
Mar 24 #PHP
php创建多级目录的方法
Mar 24 #PHP
php简单获取目录列表的方法
Mar 24 #PHP
php简单获取文件扩展名的方法
Mar 24 #PHP
php控制文件下载速度的方法
Mar 24 #PHP
使用 PHPStorm 开发 Laravel
Mar 24 #PHP
You might like
PHP新手上路(十二)
2006/10/09 PHP
thinkPHP中分页用法实例分析
2015/12/26 PHP
PHP 图片合成、仿微信群头像的方法示例
2019/10/25 PHP
Javascript学习笔记1 数据类型
2010/01/11 Javascript
深入document.write()与HTML4.01的非成对标签的详解
2013/05/08 Javascript
深入探密Javascript数组方法
2015/01/08 Javascript
使用RequireJS优化JavaScript引用代码的方法
2015/07/01 Javascript
jQuery中 delegate使用的问题
2015/07/03 Javascript
js实现页面跳转的五种方法推荐
2016/03/10 Javascript
前端框架Vue.js构建大型应用浅析
2016/09/12 Javascript
bootstrap jquery dataTable 异步ajax刷新表格数据的实现方法
2017/02/10 Javascript
Node+Express+MongoDB实现登录注册功能实例
2017/04/23 Javascript
详解VUE中v-bind的基本用法
2017/07/13 Javascript
使用Node.js实现ORM的一种思路详解(图文)
2017/10/24 Javascript
在vue使用clipboard.js进行一键复制文本的实现示例
2019/01/15 Javascript
[46:44]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第二场 3月7日
2021/03/11 DOTA
关于Python元祖,列表,字典,集合的比较
2017/01/06 Python
详解Python异常处理中的Finally else的功能
2017/12/29 Python
python微信撤回监测代码
2019/04/29 Python
python的sorted用法详解
2019/06/25 Python
解析Tensorflow之MNIST的使用
2020/06/30 Python
python实现二分查找算法
2020/09/18 Python
教你使用Sublime text3搭建Python开发环境及常用插件安装另分享Sublime text3最新激活注册码
2020/11/12 Python
Net-A-Porter美国官网:全球时尚奢侈品名站
2017/02/11 全球购物
美国山地自行车、露营、户外装备和服装购物网站:Aventuron
2018/05/05 全球购物
NFL Game Pass欧洲:在线观看NFL比赛直播和点播,以高清质量播放
2018/08/30 全球购物
碧欧泉法国官网:Biotherm法国
2019/10/23 全球购物
临床医学应届生求职信
2013/11/06 职场文书
总经理的岗位职责
2014/02/23 职场文书
开学典礼策划方案
2014/05/28 职场文书
2014年小学重阳节活动策划方案
2014/09/16 职场文书
2014教师党员自我评议总结
2014/09/19 职场文书
无保留意见审计报告
2015/06/05 职场文书
护士自荐信范文(2016推荐篇)
2016/01/28 职场文书
优秀的商业计划书,让融资一步到位
2019/05/07 职场文书
实体类或对象序列化时,忽略为空属性的操作
2021/06/30 Java/Android