PHP automatically jumps to pages in different regions based on IP addresses

Source: Internet
Author: User
To test, you need to download an IP database, and then save the php code I sent to the php file, and put the javascript code in the html file, to add more cities, you only need to copy elseif in javascript code several times to download an IP database, and then save the php code I sent as a php file, javascript code is put into the html file. to add more cities, you only need to copy else if in javascript code several times.
PHP code:
// Php ip retrieval algorithm
$ User_IP = ($ _ SERVER ["HTTP_VIA"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]: $ _ SERVER ["REMOTE_ADDR"];
$ User_IP = ($ user_IP )? $ User_IP: $ _ SERVER ["REMOTE_ADDR"];
// Echo $ user_IP;
// ============================================
//
// Function: obtains the real address of an IP address.
// Parameter: $ ip-ip address
//
// ============================================
Function convertip ($ ip ){
// IP data file path, which can be modified as needed
$ Dat_path = 'IP. dat ';
// Check the IP address
If (! Ereg ("^ ([0-9] {1, 3}.) {3} [0-9] {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 ';
}
// Calculate the number of integers by decomposing IP addresses.
$ Ip = explode ('.', $ ip );
$ IpNum = $ ip [0] X 16777216 + $ ip [1] * 65536 + $ ip [2] * 256 + $ ip [3];
// Obtain the start and end locations of the IP address data index.
$ 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 the binary search method to search for matched IP records from index records
While ($ ip1num> $ ipNum | $ ip2num <$ ipNum ){
$ Middle = intval ($ EndNum + $ BeginNum)/2 );
// Read 4 bytes from the offset pointer to the index position
Fseek ($ fd, $ ipbegin + 7 * $ Middle );
$ IpData1 = fread ($ fd, 4 );
If (strlen ($ ipData1) <4 ){
Fclose ($ fd );
Return 'system error ';
}
// The extracted data is converted into an integer. if the data is negative, the power of 2 is added.
$ Ip1num = implode (", unpack ('L', $ ipData1 ));
If ($ ip1num <0) $ ip1num + = pow (2, 32 );
// If the number of extracted long integers is greater than our IP address, modify the end position for the next loop.
If ($ ip1num> $ ipNum ){
$ EndNum = $ Middle;
Continue;
}
// Obtain the next index after obtaining the previous index
$ 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 );
// Unknown prompt not found
If ($ ip2num <$ ipNum ){
If ($ Middle = $ BeginNum ){
Fclose ($ fd );
Return 'unknown ';
}
$ BeginNum = $ Middle;
}
}
// The following code is dizzy. if you do not understand it, read it 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 )){
$ 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 );
// Return the result after the replacement operation is completed.
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;
}
?>

Javascript code:

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.