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 相关文章推荐
BBS(php &amp; mysql)完整版(五)
Oct 09 PHP
php 404错误页面实现代码
Jun 22 PHP
上传文件先创建目录 再上传到目录里面去
Dec 29 PHP
PHP5中虚函数的实现方法分享
Apr 20 PHP
PHP 第三节 变量介绍
Apr 28 PHP
php防注入及开发安全详细解析
Aug 09 PHP
一个简单至极的PHP缓存类代码
Oct 23 PHP
汇总PHPmailer群发Gmail的常见问题
Feb 24 PHP
php编程每天必学之验证码
Mar 03 PHP
PHP中list方法用法示例
Dec 01 PHP
PHP关键特性之命名空间实例详解
May 06 PHP
PHP给前端返回一个JSON对象的实例讲解
May 31 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
关于Sphinx创建全文检索的索引介绍
2013/06/25 PHP
PHP递归删除目录几个代码实例
2014/04/21 PHP
THinkPHP获取客户端IP与IP地址查询的方法
2016/11/14 PHP
载入进度条 效果
2006/07/08 Javascript
JavaScript 不只是脚本
2007/05/30 Javascript
ExtJS 入门
2010/10/29 Javascript
30个最好的jQuery 灯箱插件分享
2011/04/25 Javascript
JavaScript使用二分查找算法在数组中查找数据的方法
2015/04/07 Javascript
ie下js不执行的几种可能
2017/02/28 Javascript
React-native桥接Android原生开发详解
2018/01/17 Javascript
详解nvm管理多版本node踩坑
2019/07/26 Javascript
Vue 3.0 前瞻Vue Function API新特性体验
2019/08/12 Javascript
微信小程序中data-key属性之数据传输(经验总结)
2020/08/22 Javascript
vue+vant实现购物车全选和反选功能
2020/11/17 Vue.js
解读Python编程中的命名空间与作用域
2015/10/16 Python
非递归的输出1-N的全排列实例(推荐)
2017/04/11 Python
python爬虫获取小区经纬度以及结构化地址
2018/12/30 Python
浅析Python 读取图像文件的性能对比
2019/03/07 Python
python学生信息管理系统实现代码
2019/12/17 Python
tensorboard实现同时显示训练曲线和测试曲线
2020/01/21 Python
python属于哪种语言
2020/08/16 Python
HTML5 实战PHP之Web页面表单设计
2011/10/09 HTML / CSS
美体小铺英国官网:The Body Shop英国
2017/01/24 全球购物
JD Sports丹麦:英国领先的运动时尚零售商
2020/11/24 全球购物
.NET remoting的两种通道是什么
2016/05/31 面试题
组工干部对照检查材料
2014/08/25 职场文书
高中毕业典礼演讲稿
2014/09/09 职场文书
县政府办公室领导班子对照检查材料思想汇报
2014/09/28 职场文书
廉政文化进校园广播稿
2014/10/20 职场文书
学校党风廉政建设调研报告
2015/01/01 职场文书
区域销售经理岗位职责
2015/04/02 职场文书
2015年政治教研组工作总结
2015/07/22 职场文书
消防安全培训工作总结
2015/10/23 职场文书
Python的flask接收前台的ajax的post数据和get数据的方法
2021/04/12 Python
CSS巧用渐变实现高级感背景光动画
2021/12/06 HTML / CSS
TV动画《间谍过家家》公开PV
2022/03/20 日漫