Discuz 5.0 Read the pure IP database function Analysis _php tutorial

Source: Internet
Author: User
Tags fread get ip unpack
Discuz 5.0 is not using their own IP data, but the use of pure IP data format, access to the pure IP database a little bit of trouble, its storage format is very special and interesting, specific format analysis refer to the following two links, other languages to implement the link at the end of the reference article.

"Pure IP database format detailed"
Link One: http://blog.csdn.net/heiyeshuwu/archive/2006/05/12/725675.aspx
Link two: http://lumaqq.linuxsir.org/article/qqwry_format_detail.html

Pure IP Database official website: http://www.cz88.net/ip/
Pure 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 and is interested in reading the analysis in detail. (The following code I made a simple modification, more convenient to read, the core is not modified)


//===================================
//
Function: IP address get 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 IP Data file
if (! $fd = @fopen ($dat _path, ' RB ')) {
Return ' IP date file not exists or access denied ';
}

Decomposition IP for operation, resulting in shaping number
$ip = Explode ('. ', $IP);
$ipNum = $ip [0] * 16777216 + $ip [1] * 65536 + $ip [2] * + + $ip [3];

Get IP Data index start and end locations
$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;

Search for matching IP records from index records using the binary lookup method
while ($ip 1num> $ipNum | | $ip 2num< $ipNum) {
$Middle = Intval (($EndNum + $BeginNum)/2);

Offset pointer to index position read 4 bytes
Fseek ($FD, $ipbegin + 7 * $Middle);
$ipData 1 = fread ($FD, 4);
if (strlen ($ipData 1) < 4) {
Fclose ($FD);
Return ' System Error ';
}
Extracted data transformation growth shaping, if the data is negative, plus 2 of the power of 32
$ip 1num = Implode (', Unpack (' L ', $ipData 1));
if ($ip 1num < 0) $ip 1num + = POW (2, 32);

Extract the number of long integers greater than our IP address then modify the end position for the next loop
if ($ip 1num > $ipNum) {
$EndNum = $Middle;
Continue
}

Remove an index after the last index is taken
$DataSeek = Fread ($FD, 3);
if (strlen ($DataSeek) < 3) {
Fclose ($FD);
Return ' System Error ';
}
$DataSeek = Implode (', Unpack (' L ', $DataSeek. chr (0)));
Fseek ($FD, $DataSeek);
$ipData 2 = fread ($FD, 4);
if (strlen ($ipData 2) < 4) {
Fclose ($FD);
Return ' System Error ';
}
$ip 2num = Implode (', Unpack (' L ', $ipData 2));
if ($ip 2num < 0) $ip 2num + = POW (2, 32);

No hints found
if ($ip 2num < $ipNum) {
if ($Middle = = $BeginNum) {
Fclose ($FD);
Return ' Unknown ';
}
$BeginNum = $Middle;
}
}

The following code read dizzy, did not read clearly, interested in reading slowly
$ipFlag = Fread ($FD, 1);
if ($ipFlag = = Chr (1)) {
$ipSeek = Fread ($FD, 3);
if (strlen ($ipSeek) < 3) {
Fclose ($FD);
Return ' System Error ';
}
$ipSeek = Implode (', Unpack (' L ', $ipSeek. chr (0)));
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)) {
$AddrSeek 2 = fread ($FD, 3);
if (strlen ($AddrSeek 2) < 3) {
Fclose ($FD);
Return ' System Error ';
}
$AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
Fseek ($FD, $AddrSeek 2);
} else {
Fseek ($FD,-1, seek_cur);
}

while ($char = Fread ($FD, 1))! = Chr (0))
$ipAddr 2. = $char;

$AddrSeek = Implode (', Unpack (' L ', $AddrSeek. chr (0)));
Fseek ($FD, $AddrSeek);

while ($char = Fread ($FD, 1))! = Chr (0))
$ipAddr 1. = $char;
} else {
Fseek ($FD,-1, seek_cur);
while ($char = Fread ($FD, 1))! = Chr (0))
$ipAddr 1. = $char;

$ipFlag = Fread ($FD, 1);
if ($ipFlag = = Chr (2)) {
$AddrSeek 2 = fread ($FD, 3);
if (strlen ($AddrSeek 2) < 3) {
Fclose ($FD);
Return ' System Error ';
}
$AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
Fseek ($FD, $AddrSeek 2);
} else {
Fseek ($FD,-1, seek_cur);
}
while ($char = Fread ($FD, 1))! = Chr (0)) {
$ipAddr 2. = $char;
}
}
Fclose ($FD);

The result is returned after the corresponding substitution operation is finally made
if (Preg_match ('/http/i ', $ipAddr 2)) {
$ipAddr 2 = ";
}
$IPADDR = "$ipAddr 1 $ipAddr 2";
$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;
}


//========================
//
invocation example (FAST)
//
//========================

echo convertip (' 219.238.235.10 ');
Output: Beijing Telecom Pass

echo convertip (' 23.56.82.12 ');
Output: IANA

echo convertip (' 250.69.52.0 ');
Output: IANA reserved address

echo convertip (' 238.69.52.0 ');
Output: IANA reserved addresses for multicast

echo convertip (' 192.168.0.1 ');
Output: LAN each other and you on the same intranet

echo convertip (' 255.255.255.255 ');
Output: Pure network November 20, 2006 IP data

?>

http://www.bkjia.com/PHPjc/318158.html www.bkjia.com true http://www.bkjia.com/PHPjc/318158.html techarticle Discuz5.0 not use their own IP data, but the use of pure IP data format, access to pure IP database a little bit of trouble, its storage format is more special and interesting, specific format ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.