Php: Obtain the real IP address of QQwry. dat-PHP source code

Source: Internet
Author: User
Tags unpack
Ec (2); & lt ;? Php tutorial function: ip address obtaining real address function parameters: $ ip-ip address functionconvertip ($ ip) {$ dat_path... QQWry. Dat; script ec (2); script

// ============================================
//
// Function: obtains the real address of an IP address.
// Parameter: $ ip-ip Address
//
// ============================================
Function convertip ($ ip ){
$ Dat_path = '../QQWry. Dat ';
If (! Preg_match ("/^ d {1, 3}. d {1, 3}. d {1, 3}. d {1, 3} $/", $ ip )){
Return 'IP Address error ';
}
If (! $ Fd = @ fopen ($ dat_path, 'rb ')){
Return 'IP date file not exists or access denied ';
}
$ Ip = explode ('.', $ ip );
$ IpNum = $ ip [0] x 16777216 + $ ip [1] * 65536 + $ ip [2] * 256 + $ ip [3];
$ 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;
While ($ ip1num> $ ipNum | $ ip2num <$ ipNum ){
$ Middle = intval ($ EndNum + $ BeginNum)/2 );
Fseek ($ fd, $ ipbegin + 7 * $ Middle );
$ IpData1 = fread ($ fd, 4 );
If (strlen ($ ipData1) <4 ){
Fclose ($ fd );
Return 'System error ';
}
$ Ip1num = implode ('', unpack ('l', $ ipData1 ));
If ($ ip1num <0) $ ip1num + = pow (2, 32 );

If ($ ip1num> $ ipNum ){
$ EndNum = $ Middle;
Continue;
}
$ DataSeek = fread ($ fd, 3 );
If (strlen ($ DataSeek) <3 ){
Fclose ($ fd );
Return 'System error ';
}
$ DataSeek = implode ('', unpack ('l', $ DataSeek. chr (0 )));
Fseek ($ fd, $ DataSeek );
$ IpData2 = fread ($ fd, 4 );
If (strlen ($ ipData2) <4 ){
Fclose ($ fd );
Return 'System error ';
}
$ Ip2num = implode ('', unpack ('l', $ ipData2 ));
If ($ ip2num <0) $ ip2num + = pow (2, 32 );
If ($ ip2num <$ ipNum ){
If ($ Middle = $ BeginNum ){
Fclose ($ fd );
Return 'unknown ';
}
$ BeginNum = $ Middle;
}
}
$ 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 )){
$ 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;
$ 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;
}
?>


Instructions for use:
1. First save the above Code as a PHP file, and then download the pure IP database tutorial QQWry online. dat: Modify the database connection path code "$ dat_path = 'in the preceding function '.. /QQWry. dat ';";
2. Call a function:
Echo convertip ($ navinfor ['infoip']);
?>

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.