以下代码摘自 Discuz! X2 的 function_misc.php部分,功能和使用Python查询纯真IP 功能一致。

  1<?php
  2function convertip_full($ip, $ipdatafile) {
  3    if(!$fd = @fopen($ipdatafile, 'rb')) {
  4        return '- Invalid IP data file';
  5    }
  6    $ip = explode('.', $ip);
  7    $ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
  8    if(!($DataBegin = fread($fd, 4)) || !($DataEnd = fread($fd, 4)) ) return;
  9    @$ipbegin = implode('', unpack('L', $DataBegin));
 10    if($ipbegin < 0) $ipbegin += pow(2, 32);
 11    @$ipend = implode('', unpack('L', $DataEnd));
 12    if($ipend < 0) $ipend += pow(2, 32);
 13    $ipAllNum = ($ipend - $ipbegin) / 7 + 1;
 14    $BeginNum = $ip2num = $ip1num = 0;
 15    $ipAddr1 = $ipAddr2 = '';
 16    $EndNum = $ipAllNum;
 17    while($ip1num > $ipNum || $ip2num < $ipNum) {
 18        $Middle= intval(($EndNum + $BeginNum) / 2);
 19        fseek($fd, $ipbegin + 7 * $Middle);
 20        $ipData1 = fread($fd, 4);
 21        if(strlen($ipData1) < 4) {
 22            fclose($fd);
 23            return '- System Error';
 24        }
 25        $ip1num = implode('', unpack('L', $ipData1));
 26        if($ip1num < 0) $ip1num += pow(2, 32);
 27        if($ip1num > $ipNum) {
 28            $EndNum = $Middle;
 29            continue;
 30        }
 31        $DataSeek = fread($fd, 3);
 32        if(strlen($DataSeek) < 3) {
 33            fclose($fd);
 34            return '- System Error';
 35        }
 36        $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
 37        fseek($fd, $DataSeek);
 38        $ipData2 = fread($fd, 4);
 39        if(strlen($ipData2) < 4) {
 40            fclose($fd);
 41            return '- System Error';
 42        }
 43        $ip2num = implode('', unpack('L', $ipData2));
 44        if($ip2num < 0) $ip2num += pow(2, 32);
 45        if($ip2num < $ipNum) {
 46            if($Middle == $BeginNum) {
 47                fclose($fd);
 48                return '- Unknown';
 49            }
 50            $BeginNum = $Middle;
 51        }
 52    }
 53    $ipFlag = fread($fd, 1);
 54    if($ipFlag == chr(1)) {
 55        $ipSeek = fread($fd, 3);
 56        if(strlen($ipSeek) < 3) {
 57            fclose($fd);
 58            return '- System Error';
 59        }
 60        $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
 61        fseek($fd, $ipSeek);
 62        $ipFlag = fread($fd, 1);
 63    }
 64    if($ipFlag == chr(2)) {
 65        $AddrSeek = fread($fd, 3);
 66        if(strlen($AddrSeek) < 3) {
 67            fclose($fd);
 68            return '- System Error';
 69        }
 70        $ipFlag = fread($fd, 1);
 71        if($ipFlag == chr(2)) {
 72            $AddrSeek2 = fread($fd, 3);
 73            if(strlen($AddrSeek2) < 3) {
 74                fclose($fd);
 75                return '- System Error';
 76            }
 77            $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
 78            fseek($fd, $AddrSeek2);
 79        } else {
 80            fseek($fd, -1, SEEK_CUR);
 81        }
 82        while(($char = fread($fd, 1)) != chr(0))
 83        $ipAddr2 .= $char;
 84        $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
 85        fseek($fd, $AddrSeek);
 86        while(($char = fread($fd, 1)) != chr(0))
 87        $ipAddr1 .= $char;
 88    } else {
 89        fseek($fd, -1, SEEK_CUR);
 90        while(($char = fread($fd, 1)) != chr(0))
 91        $ipAddr1 .= $char;
 92        $ipFlag = fread($fd, 1);
 93        if($ipFlag == chr(2)) {
 94            $AddrSeek2 = fread($fd, 3);
 95            if(strlen($AddrSeek2) < 3) {
 96                fclose($fd);
 97                return '- System Error';
 98            }
 99            $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
100            fseek($fd, $AddrSeek2);
101        } else {
102            fseek($fd, -1, SEEK_CUR);
103        }
104        while(($char = fread($fd, 1)) != chr(0))
105        $ipAddr2 .= $char;
106    }
107    fclose($fd);
108    if(preg_match('/http/i', $ipAddr2)) {
109        $ipAddr2 = '';
110    }
111    $ipaddr = "$ipAddr1 $ipAddr2";
112    $ipaddr = preg_replace('/CZ88.NET/is', '', $ipaddr);
113    $ipaddr = preg_replace('/^s*/is', '', $ipaddr);
114    $ipaddr = preg_replace('/s*$/is', '', $ipaddr);
115    if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
116        $ipaddr = '- Unknown';
117    }
118    return '- '.$ipaddr;
119}
120?>