Get IP Address
Copy codeThe Code is as follows:
<? Php
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
Copy codeThe 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;
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://www.jb51.net/softs/10529.html
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:
Copy codeThe 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
Copy codeThe Code is as follows:
Var city = '<? Echo ipCity ($ xp_UserIp);?> ';
// Jump to the specified page according to the IP address of all cities
Copy codeThe 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:
Copy codeThe Code is as follows:
<Script src = "/ipcity/ipaddress. php" type = "text/javascript" language = "javascript"> </script>
Is refreshing the page as expected?
The above Code simply determines that the ip address will jump there. If it appears, I want to jump all the IP addresses in Hunan to Changsha, like Hengyang, Liu Yang, yang Zhengyang will have to perform multiple parent line operations. For details, see the next set.