公告:本站提供编程开发方面的技术交流与分享,打造最佳教程网,希望能为您排忧解难!

PHP教程最新的IP地址查询工具源码

最新的IP地址查询工具源码

更新时间:2013-05-21 21:26:25 |

1<?php

/**

3 * @author merauy@gmail.com

4 * @Copyright(c)2010~2014

5 * @date 2010-10-01

6 * @version 1.0.0

7 * @direction IP地址查询

8 * @演示地址:http://www.goodxyx.com/tool/ipsearch.php

9 * @源码地址:http://www.goodxyx.com/php/article/cei430.html

10 * @demo 使用方法:

11 *          $oCzyxIp = new Czyx_Ip();

12 *          $sIp     = '192.168.1.1';

13 *          $aArea   = $oCzyxIp->search($sIp);

14 *          print_r($aArea); //看看是不是您想要的结果

15 *                           //兼容最新的IP纯真库

16 *                           //注意:查询出来的结果编码是gbk的

17 *           &bbsp;               // 如果你的网站是UTF-8进行简单转码即可

18 *

19 */

class Czyx_Ip{

21    protected $_startIp ;

22    protected $_endIp ;

23    protected $_area1 ;

24    protected $_area2 ;

25    protected $_fp    ;

26    protected $_firstStartIp ;

27    protected $_lastStartIp  ;

28    protected $_recordCount  ;

29    protected $_dataFile ;

30    protected $_prepare  ;

31

    public function __construct(){

33        //这里只要把IP库文件路径

34        $this->_dataFile =

35        realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..')

36        .DIRECTORY_SEPARATOR.'Data'

37        .DIRECTORY_SEPARATOR.'czyx_ip.dat';

38    }

39

    public function setDataFile($file){

41        $this->_dataFile = $file ;

42        return $this ;

43    }

44

    public function __destruct(){

46        $this->close();

47    }

48

    public function close(){

50        $this->_prepare = null ;

        if($this->_fp){

52            fclose($this->_fp);

53            $this->_fp = null ;

54        }

55    }

56

    public function prepare(){

        if(!$this->_fp){

59            $this->_fp = fopen($this->_dataFile'rb');

            if(!$this->_fp){

61                throw new Exception('');

62            }

            if(!$this->_recordCount){

64                $this->_firstStartIp = $this->_getNextLong() ;

65                $this->_lastStartIp  = $this->_getNextLong() ;

66                $this->_recordCount  =

67                    floor(($this->_lastStartIp - $this->_firstStartIp )/7);

                if ($this->_recordCount <= 1){

69                    $this->close();

70                    throw new Exception('');

71                }

72            }

73        }

74        $this->_prepare = true ;

75        return $this ;

76    }

77

    protected function _getNextLong(){

79        $result = unpack('V'fread($this->_fp4));

80        return array_shift($result);

81    }

82

    protected function _getNextLong3(){

84        $result = unpack('V'fread($this->_fp3).chr(0));

85        return array_shift($result);

86    }

87

    public function search($searchIp='') {

89        $formatIp =  $this->_formatIp($searchIp);

        if($formatIp === false){

91            $this->_area1   = 'unknown' ;

92            $this->_area2   = '' ;

93            $this->_startIp = $this->_formatIp('0.0.0.0') ;

94            $this->_endIp   = $this->_startIp ;

95            return $this->getLastSearchInfo();

96        }

97

        if(!$this->_prepare){

99            $this->prepare()->_prepare = null ;

100        }

101

102        $begin = 0;

103        $end   = $this->_recordCount;

        while ($begin < $end-1){

105            $recordNofloor(($begin + $end)*0.5);

            if ($formatIp == $this->_moveToStartIp($recordNo)

107                ->_readStartIp()

108                ->_startIp

            ){

110                $begin = $recordNo ;

111                return $this->_readEndIp()

112                    ->_readArea()

113                    ->getLastSearchInfo();

114            }

            if ($formatIp >> $this->_startIp){

116                $begin$recordNo;

117            }

            else{

119                $end$recordNo;

120            }

121        }

        if($this->_moveToStartIp($begin)

123                ->_readStartIp()->_startIp <= $formatIp

124            && $this->_readEndIp()->_endIp >= $formatIp

        ){

126                $this->_readArea();

        }else{

128            $this->_area1 = 'unknown' ;

129            $this->_area2 = '' ;

130        }

        if(!$this->_prepare){

132            $this->close();

133        }

134        return $this->getLastSearchInfo();

135    }

136

    protected function _toRealIp($cacheIp){

138        $a = unpack('N'$cacheIp) ;//相当于 unpack('V', strrev($ip));

139                               //N 一个 32 位短整形,网络字节序(大头在前)

140                               //V 一个VAX字节序(小头在前)的 32 位短整数

141        return long2ip(array_shift($a));

142    }

143

    protected function _formatIp($realIp){

145        $formatIp =  ip2long($realIp);

        if($formatIp == -1 || $formatIp === false){

147            return false ;

148        }

149        return pack('N'$formatIp);

150    }

151

    public function getLastSearchInfo(){

        return array(

154            'start_ip' => $this->_toRealIp($this->_startIp),

155            'end_ip'   => $this->_toRealIp($this->_endIp),

156            'area1'    =>

157                    $this->_area1 == ' CZ88.NET' ?

158                    'unkonwn' : trim($this->_area1),

159            'area2'    =>

160                    $this->_area2 == ' CZ88.NET' ?

161                    '' : trim($this->_area2),

162        );

163    }

164

    protected function _moveToStartIp($recordNo){

        fseek(

167            $this->_fp,

168            $this->_firstStartIp + $recordNo * 7,

169            SEEK_SET

170        );

171        return $this ;

172    }

173

    protected function _readStartIp(){

175        $this->_startIp = strrev(fread($this->_fp4));

176        return $this ;

177    }

178

    protected function _readEndIp(){

180        fseek($this->_fp , $this->_getNextLong3()SEEK_SET ) ;

181        $this->_endIp = strrev(fread($this->_fp4));

182    return $this ;

183    }

184

    protected function _readArea(){

186        $c = fgetc($this->_fp) ;

187        $model = ord($c);

188

        if($model == 1){

190            fseek($this->_fp , $this->_getNextLong3() , SEEK_SET ) ;

191            return $this->_readArea();

192        }

193

        if($model == 2){

195            $offset       = $this->_getNextLong3() ;

196            $this->_area2 = $this->_getNextArea();

197            fseek($this->_fp , $offset , SEEK_SET ) ;

198            $this->_area1 = $this->_getNextArea();

        }else if($model == 0){

200            echo 'ERROR----ERROR';exit;

        }else{

202            $this->_area1 = $c.$this->_getAreaStr() ;

203            $this->_area2 = $this->_getNextArea() ;

204        }

205

206        return $this ;

207    }

208

    protected function _getNextArea(){

210        $c = fgetc($this->_fp) ;

211        $model = ord($c);

        if($model == 1 || $model == 2){

213            fseek($this->_fp , $this->_getNextLong3()SEEK_SET ) ;

214            return $this->_getNextArea();

215        }

216

217

218

        if($model == 0){

220            return '' ;

221        }

222

223        return $c.$this->_getAreaStr();

224    }

225

    protected function _getAreaStr(){

227        $str='' ;

        while (1) {

229            $c = fgetc($this->_fp) ;

            if(ord($c[0])== 0 ){

231                break ;

232            }

233            $str.= $c ;

234        }

235        return $str ;

236    }

237

    public function export($file){

239        $fp = fopen($file'w');

        if(!$fp){

241            throw new Excption('');

242        }

243

        if($this->_fp){

245            $offset = ftell($this->_fp);

246            $isOpen = true ;

        }else{

248            $isOpen = false ;

249            $this->prepare(1);

250        }

251

252        $begin = 0 ;

253        $end = $this->_recordCount ;

        while($begin < $end){

255            $info = $this->_moveToStartIp($begin++)

256                ->_readStartIp()

257                ->_readEndIp()

258                ->_readArea()

259                ->getLastSearchInfo();

            fwrite(

261                $fp,

262                $info['start_ip']

263                .','.$info['end_ip']

264                .','.$info['area1']

265                .','.$info['area2']."\n"

266            );

267        }

268        fclose($fp);

269

        if($isOpen){

271            fseek($this->_fp$offsetSEEK_SET);

        }else{

273            fclose($this->_fp);

274            $this->_fp = null ;

275        }

276

277        return $this ;

278    }

279}

最佳教程网

最大的技术交流平台 www.goodxyx.com© CopyRight 2011-2013, All Rights Reserved

浙ICP备11033019号