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 相关文章推荐
删除无限分类并同时删除它下面的所有子分类的方法
Aug 08 PHP
php SQL Injection with MySQL
Feb 27 PHP
php去掉字符串的最后一个字符附substr()的用法
Mar 23 PHP
比较详细PHP生成静态页面教程
Jan 10 PHP
Notice: Trying to get property of non-object problem(PHP)解决办法
Mar 11 PHP
PHP array_multisort() 函数的深入解析
Jun 20 PHP
PHP strip_tags()去除HTML、XML以及PHP的标签介绍
Feb 18 PHP
改写ThinkPHP的U方法使其路由下分页正常
Jul 02 PHP
php中Socket创建与监听实现方法
Jan 05 PHP
在WordPress中使用PHP脚本来判断访客来自什么国家
Dec 10 PHP
php实现的AES加密类定义与用法示例
Jan 29 PHP
php 与 nginx 的处理方式及nginx与php-fpm通信的两种方式
Sep 28 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-fpm静态和动态执行方式的比较
2016/11/09 PHP
php_pdo 预处理语句详解
2016/11/21 PHP
laravel-admin的图片删除实例
2019/09/30 PHP
在laravel框架中实现封装公共方法全局调用
2019/10/14 PHP
PHP读取文件或采集时解决中文乱码
2021/03/09 PHP
TFDN图片播放器 不错自动播放
2006/10/03 Javascript
js图片处理示例代码
2014/05/12 Javascript
单元选择合并变色示例代码
2014/05/26 Javascript
Node.js如何使用Diffie-Hellman密钥交换算法详解
2017/09/05 Javascript
jQuery UI 实例讲解 - 日期选择器(Datepicker)
2017/09/18 jQuery
Node.js上传文件功能之服务端如何获取文件上传进度
2018/02/05 Javascript
Vue2.0 http请求以及loading展示实例
2018/03/06 Javascript
Angular6 Filter实现页面搜索的示例代码
2018/12/02 Javascript
用js编写留言板
2020/03/17 Javascript
Vue 组件注册全解析
2020/12/17 Vue.js
使用Python编写一个简单的tic-tac-toe游戏的教程
2015/04/16 Python
python回调函数中使用多线程的方法
2017/12/25 Python
Python图像处理之图像的缩放、旋转与翻转实现方法示例
2019/01/04 Python
python基于socket进行端口转发实现后门隐藏的示例
2019/07/25 Python
Python 如何批量更新已安装的库
2020/05/26 Python
Python模拟登录和登录跳转的参考示例
2020/10/30 Python
关于HTML5的22个初级技巧(图文教程)
2012/06/21 HTML / CSS
HTML5 文件域+FileReader 分段读取文件并上传到服务器
2017/10/23 HTML / CSS
印尼综合在线预订网站:Tiket.com(机票、酒店、火车、租车和娱乐)
2018/10/11 全球购物
德国汽车零件和汽车配件网上商店:kfzteile24
2018/11/14 全球购物
澳大利亚著名的纺织品品牌:Canningvale
2020/05/05 全球购物
戴尔荷兰官方网站:Dell荷兰
2020/10/04 全球购物
面试求职的个人自我评价
2013/11/16 职场文书
外联部演讲稿
2014/05/24 职场文书
2014国庆节餐厅促销活动策划方案
2014/09/16 职场文书
2014年预备党员群众路线教育实践活动对照检查材料思想汇报
2014/10/02 职场文书
2014年最新离婚协议书范本
2014/10/11 职场文书
体检通知范文
2015/04/21 职场文书
医院中层管理人员培训心得体会
2016/01/11 职场文书
redis requires ruby version2.2.2的解决方案
2021/07/15 Redis
Python可变集合和不可变集合的构造方法大全
2021/12/06 Python