PHP code for City switching or redirection based on IP addresses _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP determines whether a city can be switched to another city or redirect code based on the IP address. It is quite easy to get the IP address first. The following describes two codes for getting the IP address first. you need to use the QQIP library to find the IP address that belongs to the current IP address segment and then obtain the city field and return it. It is quite easy to get the IP address first. The following describes two codes for getting the IP address first. you need to use the qq ip library to find the IP address segment that the current IP address belongs to and then obtain the city field and return it.

Get IP address

The code is as follows:

Function GetIP (){
If ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])
$ Ip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
Else if ($ _ SERVER ["HTTP_CLIENT_IP"])
$ Ip = $ _ SERVER ["HTTP_CLIENT_IP"];
Else if ($ _ SERVER ["REMOTE_ADDR"])
$ Ip = $ _ SERVER ["REMOTE_ADDR"];
Else if (getenv ("HTTP_X_FORWARDED_FOR "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
Else if (getenv ("HTTP_CLIENT_IP "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("REMOTE_ADDR "))
$ Ip = getenv ("REMOTE_ADDR ");
Else
$ Ip = "Unknown ";
Return $ ip;
}
Echo GetIP ();
?>

Method 2

The code is as follows:
Function getip ()
{
If (getenv ('http _ client_ip ')){
$ Ip = getenv ('http _ client_ip ');
} Else if (getenv ('http _ x_forwarded_for ')){
$ Ip = getenv ('http _ x_forwarded_for ');
} Else if (getenv ('remote _ addr ')){
$ Ip = getenv ('remote _ addr ');
} Else {
$ Ip = $ _ server ['remote _ addr '];
}
Return $ ip;

For more details, see: http://www.bkjia.com/phper/php/33938.htm

PHP uses IP addresses to determine the city in which a user is located
You have obtained the user IP address. Next, you can obtain the user's city based on the IP address. Before we start, we need to download a ready-made Database qq ip database.

Latest IP address database: http://8.zjdx2.crsky.com/201209/qqwry0830.rar

Usage: After decompression, QQWry. Dat is the IP address database. we create an ipcity folder and place the database below. The qq ip database is very convenient to use and has complete data. you can pay attention to official updates in time to keep the latest data. we strongly recommend that you :)
Next, create an ipaddress. php file under the ipcity directory above, and copy the following code directly. the important parts are also commented out. Section B:

The code is as follows:

/*
Function name: ipCity
Parameter description: $ userip -- user IP address
Function: PHP uses IP addresses to determine the city in which a user is located.
Author: lee
Contact: xpsem2010@gmail.com
*/
Function ipCity ($ userip ){
// IP database path. here, the pure version of qq ip database 20110405 is used.
$ Dat_path = 'qqwry. dat ';

// Determine whether the IP address is valid
If (! Ereg ("^ ([0-9] {1, 3}.) {3} [0-9] {1, 3} $", $ userip )){
Return 'IP Address invalid ';
}

// Open the IP database
If (! $ Fd = @ fopen ($ dat_path, 'RB ')){
Return 'IP data file not exists or access denied ';
}

// The explode function breaks down the IP address and returns an integer.
$ Userip = explode ('.', $ userip );
$ UseripNum = $ userip [0] X 16777216 + $ userip [1] * 65536 + $ userip [2] * 256 + $ userip [3];

// Obtain the start and end positions of the IP address index.
$ DataBegin = fread ($ fd, 4 );
$ DataEnd = fread ($ fd, 4 );
$ Useripbegin = implode ('', unpack ('L', $ DataBegin ));
If ($ useripbegin <0) $ useripbegin + = pow (2, 32 );
$ Useripend = implode ('', unpack ('L', $ DataEnd ));
If ($ useripend <0) $ useripend + = pow (2, 32 );
$ UseripAllNum = ($ useripend-$ useripbegin)/7 + 1;

$ BeginNum = 0;
$ EndNum = $ useripAllNum;

// Use the binary search method to search for matched IP address records from index records
While ($ userip1num> $ useripNum | $ userip2num <$ useripNum ){
$ Middle = intval ($ EndNum + $ BeginNum)/2 );

// Read 4 bytes from the offset pointer to the index position
Fseek ($ fd, $ useripbegin + 7 * $ Middle );
$ UseripData1 = fread ($ fd, 4 );
If (strlen ($ useripData1) <4 ){
Fclose ($ fd );
Return 'file error ';
}
// The extracted data is converted into an integer. if the data is negative, the power of 2 is added.
$ Userip1num = implode ('', unpack ('L', $ useripData1 ));
If ($ userip1num <0) $ userip1num + = 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 ($ userip1num> $ useripNum ){
$ EndNum = $ Middle;
Continue;
}

// Obtain the next index after obtaining the previous index
$ DataSeek = fread ($ fd, 3 );
If (strlen ($ DataSeek) <3 ){
Fclose ($ fd );
Return 'file error ';
}
$ DataSeek = implode ('', unpack ('L', $ DataSeek. chr (0 )));
Fseek ($ fd, $ DataSeek );
$ UseripData2 = fread ($ fd, 4 );
If (strlen ($ useripData2) <4 ){
Fclose ($ fd );
Return 'file error ';
}
$ Userip2num = implode ('', unpack ('L', $ useripData2 ));
If ($ userip2num <0) $ userip2num + = pow (2, 32 );

// Unable to find the IP address corresponding to the city
If ($ userip2num <$ useripNum ){
If ($ Middle = $ BeginNum ){
Fclose ($ fd );
Return 'no data ';
}
$ BeginNum = $ Middle;
}
}

$ UseripFlag = fread ($ fd, 1 );
If ($ useripFlag = chr (1 )){
$ UseripSeek = fread ($ fd, 3 );
If (strlen ($ useripSeek) <3 ){
Fclose ($ fd );
Return 'system error ';
}
$ UseripSeek = implode ('', unpack ('L', $ useripSeek. chr (0 )));
Fseek ($ fd, $ useripSeek );
$ UseripFlag = fread ($ fd, 1 );
}

If ($ useripFlag = chr (2 )){
$ AddrSeek = fread ($ fd, 3 );
If (strlen ($ AddrSeek) <3 ){
Fclose ($ fd );
Return 'system error ';
}
$ UseripFlag = fread ($ fd, 1 );
If ($ useripFlag = 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 ))
$ UseripAddr2. = $ char;

$ AddrSeek = implode ('', unpack ('L', $ AddrSeek. chr (0 )));
Fseek ($ fd, $ AddrSeek );

While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
$ UseripAddr1. = $ char;
} Else {
Fseek ($ fd,-1, SEEK_CUR );
While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
$ UseripAddr1. = $ char;

$ UseripFlag = fread ($ fd, 1 );
If ($ useripFlag = 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 )){
$ UseripAddr2. = $ char;
}
}
Fclose ($ fd );

// Return the City result corresponding to the IP address
If (preg_match ('/http/I', $ useripAddr2 )){
$ UseripAddr2 = '';
}
$ Useripaddr = "$ useripAddr1 $ useripAddr2 ";
$ Useripaddr = preg_replace ('/CZ88.Net/is', '', $ useripaddr );
$ Useripaddr = preg_replace ('/^ s */is', '', $ useripaddr );
$ Useripaddr = preg_replace ('/s * $/is', '', $ useripaddr );
If (preg_match ('/http/I', $ useripaddr) | $ useripaddr = ''){
$ Useripaddr = 'no data ';
}

Return $ useripaddr;
}
?>

PHP implements City switching or redirection based on IP addresses
Here, the problem is actually quite simple. it can be done with simple js. Section C is as follows:
// Jump to the specified page based on the IP address. js gets the city

The code is as follows:

Var city =' ';

// Jump to the specified page according to the IP address of all cities

The code is as follows:

If (city. indexOf ("Shanghai")> = 0 ){
Window. location. href = "http://shanghai.demo.com /";
}

Place the-segment code and the C-segment code above in the header and end of the B-segment code respectively, and then add the following code on the page to jump:

The code is as follows:

Related Article

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.