Php judges IP address location source code _ PHP Tutorial

Source: Internet
Author: User
Php determines the source code of the IP address. The following code judges the user's city based on the IP address, php judges that the IP address location source code is completely open. the following code calls us to determine the user based on the IP address, so the city. the following code judges the user's city based on the IP address, php calls to determine whether the IP address's source code is completely open

The following code judges the user's city based on the IP address. php judges that the source code of the IP address is completely open.

Function convertIp ($ ip ){
$ Return = '';
If (preg_match ("/^ d {1, 3}. d {1, 3}. d {1, 3}. d {1, 3} $/", $ ip )){
$ Iparray = explode ('.', $ ip );
If ($ iparray [0] = 10 | $ iparray [0] = 127 | ($ iparray [0] = 192 & $ iparray [1] = 168) | ($ iparray [0] = 172 & ($ iparray [1]> = 16 & $ iparray [1] <= 31 ))){
$ Return = '-LAN ';
} Elseif ($ iparray [0]> 255 | $ iparray [1]> 255 | $ iparray [2]> 255 | $ iparray [3]> 255 ){
$ Return = '-Invalid IP Address ';
} Else {
$ Ipfile = MOOPHP_ROOT. '/plugins/ipdata/wry. dat ';
If (! @ File_exists ($ ipfile )){
$ Return = convertIpFull ($ ip, $ ipfile );
}
}
}
Return $ return;
}

Function convertIpFull ($ ip, $ ipdatafile ){

If (! $ Fd = @ fopen ($ ipdatafile )){
Return '-Invalid IP data file ';
}

$ Ip = explode ('.', $ ip );
$ IpNum = $ ip [0] X 16777216 + $ ip [1] * 65536 + $ ip [2] * 256 + $ ip [3];

If (! ($ DataBegin = fread ($ fd, 4) |! ($ DataEnd = fread ($ fd, 4) return;
@ $ 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 = $ ip2num = $ ip1num = 0;
$ IpAddr1 = $ ipAddr2 = '';
$ 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;

}


When the IP address location source code is determined by the hacker, the following code calls us to determine the user's city based on the IP address...

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.