php判断ip并转跳到相应城市分站的方法 这篇文章主要介绍了php判断ip并转跳到相应城市分站的方法,实例分析了php解析url及跳转的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php判断ip并转跳到相应城市分站的方法。分享给大家供大家参考。具体实现方法如下:
?
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            53
            54
            55
            56
            57
            58
            59
            60
            61
            62
            63
            64
            65
            66
            67
            68
            69
            70
            71
            72
            73
            74
            75
            76
            77
            78
            79
            80
            81
            82
            83
            84
            85
            86
            87
            88
            89
            90
            91
            92
            93
            94
            95
            96
            97
            98
            99
            100
            101
            102
            103
            104
            105
            106
            107
            108
            109
            110
            111
            112
            113
            114
            115
            116
            117
            118
            119
            120
            121
            122
            123
            124
            125
            126
            127
            128
            129
            130
            131
            132
            133
            134
            135
            136
            137
            138
            139
            140
            141
            142
            143
            144
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            
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             
$this->country=filedataerror;
            fclose($this->fp) ;
            return 2 ;
            }
            $rangb= 0;
            $range= $recordcount;
            // match ...
            while ($rangb             
{
            $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 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;
            }
            }
            ?>
实例演示如下:
?
            1
            2
            3
            4
$qqwry=new qqwry;
            $qqwry->qqwry(60.31.95.255);
            $country = $qqwry->country;
            echo $qqwry->country.,.$qqwry->local;
希望本文所述对大家的php程序设计有所帮助。
http://www.bkjia.com/phpjc/973923.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/973923.htmltecharticlephp判断ip并转跳到相应城市分站的方法 这篇文章主要介绍了php判断ip并转跳到相应城市分站的方法,实例分析了php解析url及跳转的技巧,具有一...
   
 
   