Discuz 5.0 no longer uses its own IP data, but uses the data format of Innocence IP. Accessing the Innocence IP database is a little troublesome. Its storage format is special and interesting. For specific format analysis, please refer to the following two links. Implementation in other languages See the link at the end of the article.
"Detailed Explanation of Innocence IP Database Format"
Link 1: http://blog.csdn.net/heiyeshuwu/archive/2006/05/12/725675.aspx
Link 2: http: //lumaqq.linuxsir.org/article/qqwry_format_detail.html
Innocent IP database official website: http://www.cz88.net/ip/
Innocent IP database download: http://update. cz88.net/soft/qqwry.rar
The following function conrvertip() is located in the Discuz!5_GBK/upload/include/misc.func.php path. If you are interested, you can read and analyze it in detail.(I have made simple modifications to the code below to make it easier to read, and the core has not been modified)
//================ ====================
//
// Function: IP address gets real address function
// Parameter: $ip - IP address
// Author: [Discuz!] (C) Comsenz Inc.
//
//======================== ============
function convertip($ip) {
//IP data file path
$dat_path = 'QQWry.Dat';
/ /Check IP address
if(!preg_match("/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/", $ip)) {<> Return 'IP Address Error';
}
// Open the IP data file
if (! $ Fd = @fopen ($ dat_path, 'rb')) {
RETURN ' IP date file not exists or access denied';
}
//Decompose the IP for operation and get an integer number
$ip = explode('.', $ip);
$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
//Get IP data index start and end position
$DataBegin = fread($fd, 4);
$DataEnd = fread($fd, 4);
$ipbegin = implode('', unpack('L', $DataBegin));
if($ipbegin < 0) $ipbegin += pow(2, 32);
$ipend = implode('', unpack('L', $DataEnd));
if($ipend < 0) $ipend += pow(2, 32);
$ipAllNum = ($ipend - $ipbegin) / 7 + 1;
$BeginNum = 0;
$EndNum = $ipAllNum;
//Use binary search method to search for matching IP records from index records
while($ip1num>$ipNum || $ip2num<$ipNum) {
$Middle= intval (($EndNum + $BeginNum) / 2);
//Offset the pointer to the index position and read 4 bytes
fseek($fd, $ipbegin + 7 * $Middle);
$ipData1 = fread($fd, 4);
if(strlen($ipData1) < 4) {
fclose($fd);
return 'System Error'; }
//The extracted data is converted into a long integer. If the data is a negative number, add 2 to the 32nd power
$ip1num = implode('', unpack('L', $ipData1));
if($ip1num < 0) $ip1num += pow(2, 32);
//The extracted long integer is greater than our IP address, then modify the end position for the next cycle
if( $ip1num > $ipNum) {
$EndNum = $Middle;
continue;
$ DataSeek = fread($fd, 3);
if(strlen($DataSeek) < 3) {
fclose($fd); Seek = implode('', unpack('L', $DataSeek.chr(0)));
fseek($fd, $DataSeek);
$ipData2 = fread($fd, 4);
if(strlen($ipData2)
$ip2num = implode('', unpack('L', $ipData2));
if($ip2num < 0) $ip2num += pow(2, 32);
//Unknown prompt not found
if($ip2num < $ipNum) {
if($Middle == $BeginNum) {
fclose($fd);
return ' Unknown';
= fread($fd, 1);
if($ipFlag == chr(1)) {
$ipSeek = fread($fd, 3);
if(strlen($ipSeek) < 3) {
fclose($fd); );
fseek($fd, $ipSeek);
$ipFlag = fread($fd, 1);
}
if($ipFlag == chr(2)) {
$AddrSeek = fread($fd, 3);
if(strlen($AddrSeek) < 3) {
fclose($fd);
return 'System Error';
}
$ipFlag = fread($fd, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen ($AddrSeek2) < 3) {
('', unpack('L', $AddrSeek2 .chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while(($char = fread($fd, 1)) != chr(0))
$ipAddr2 .= $char;
$AddrSeek = implode('', unpack('L' , $AddrSeek.chr(0)));
fseek($fd, $AddrSeek);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
} else {
fseek($fd, -1, SEEK_CUR);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
$ipFlag = fread($fd, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while(($char = fread($fd, 1)) != chr(0)){
$ipAddr2 .= $char;
}
}
fclose($fd);
//最后做相应的替换操作后返回结果
if(preg_match('/http/i', $ipAddr2)) {
$ipAddr2 = '';
}
$ipaddr = "$ipAddr1 $ipAddr2";
$ipaddr = preg_replace('/CZ88.NET/is', '', $ipaddr);
$ipaddr = preg_replace('/^s*/is', '', $ipaddr);
$ipaddr = preg_replace('/s*$/is', '', $ipaddr);
if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
$ipaddr = 'Unknown';
}
return $ipaddr;
}
//========================
//
// 调用举例(速度很快)
//
//========================
echo convertip('219.238.235.10');
//输出: 北京市 电信通
echo convertip('23.56.82.12');
//输出:IANA
echo convertip('250.69.52.0');
//输出:IANA保留地址
echo convertip('238.69.52.0');
//输出:IANA保留地址 用于多点传送
echo convertip('192.168.0.1');
//输出:局域网 对方和您在同一内部网
echo convertip('255.255.255.255');
//输出:纯真网络 2006年11月20日IP数据
?>