_php tutorials for city switching or jump codes based on IP address in PHP

Source: Internet
Author: User
Tags fread get ip pow unpack
Get IP Address
Copy CodeThe 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 Two
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 address to determine the location of the user's city
The user's IP address has already been obtained, and then we get the user's city based on this 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
How to use: After decompression QQWry.Dat is we want IP address database, we create a new ipcity folder, the database is placed below. QQ IP database is very convenient to use, data is also very complete, you can pay attention to the official update to keep the data up-to-date, strongly recommend:)
Next, we create a new ipaddress.php file in the Ipcity directory above, directly copy the following code into it, the important place also commented accordingly. Segment B:
Copy CodeThe code is as follows:
/*
Function Name: ipCity
Parameter description: $userip--User IP address
function function: PHP to determine the location of the user's city by IP address
Author:lee
Contact:xpsem2010@gmail.com
*/
function IpCity ($userip) {
IP database path, here is the QQ IP database 20110405 Pure version
$dat _path = ' QQWry.dat ';
Determine if the IP address is valid
if (!ereg ("^ ([0-9]{1,3}.) {3} [0-9] {1,3}$ ", $userip)) {
Return ' IP address Invalid ';
}
Open IP Database
if (! $fd = @fopen ($dat _path, ' RB ')) {
Return ' IP data file not exists or access denied ';
}
The EXPLODE function decomposes the IP address, and the integer result is computed.
$userip = Explode ('. ', $userip);
$useripNum = $userip [0] * 16777216 + $userip [1] * 65536 + $userip [2] * + + $userip [3];
Get IP Address index start and end locations
$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;
Search for matching IP address records from index records using the binary lookup method
while ($userip 1num> $useripNum | | $userip 2num< $useripNum) {
$Middle = Intval (($EndNum + $BeginNum)/2);
Offset pointer to index position read 4 bytes
Fseek ($FD, $useripbegin + 7 * $Middle);
$useripData 1 = fread ($FD, 4);
if (strlen ($useripData 1) < 4) {
Fclose ($FD);
Return ' File Error ';
}
Extracted data transformation growth shaping, if the data is negative, plus 2 of the power of 32
$userip 1num = Implode (', Unpack (' L ', $useripData 1));
if ($userip 1num < 0) $userip 1num + = POW (2, 32);
Extract the number of long integers greater than our IP address then modify the end position for the next loop
if ($userip 1num > $useripNum) {
$EndNum = $Middle;
Continue
}
Remove an index after the last index is taken
$DataSeek = Fread ($FD, 3);
if (strlen ($DataSeek) < 3) {
Fclose ($FD);
Return ' File Error ';
}
$DataSeek = Implode (', Unpack (' L ', $DataSeek. chr (0)));
Fseek ($FD, $DataSeek);
$useripData 2 = fread ($FD, 4);
if (strlen ($useripData 2) < 4) {
Fclose ($FD);
Return ' File Error ';
}
$userip 2num = Implode (', Unpack (' L ', $useripData 2));
if ($userip 2num < 0) $userip 2num + = POW (2, 32);
IP address not found for the city
if ($userip 2num < $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)) {
$AddrSeek 2 = fread ($FD, 3);
if (strlen ($AddrSeek 2) < 3) {
Fclose ($FD);
Return ' System Error ';
}
$AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
Fseek ($FD, $AddrSeek 2);
} else {
Fseek ($FD,-1, seek_cur);
}
while ($char = Fread ($FD, 1))! = Chr (0))
$useripAddr 2. = $char;
$AddrSeek = Implode (', Unpack (' L ', $AddrSeek. chr (0)));
Fseek ($FD, $AddrSeek);
while ($char = Fread ($FD, 1))! = Chr (0))
$useripAddr 1. = $char;
} else {
Fseek ($FD,-1, seek_cur);
while ($char = Fread ($FD, 1))! = Chr (0))
$useripAddr 1. = $char;
$useripFlag = Fread ($FD, 1);
if ($useripFlag = = Chr (2)) {
$AddrSeek 2 = fread ($FD, 3);
if (strlen ($AddrSeek 2) < 3) {
Fclose ($FD);
Return ' System Error ';
}
$AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
Fseek ($FD, $AddrSeek 2);
} else {
Fseek ($FD,-1, seek_cur);
}
while ($char = Fread ($FD, 1))! = Chr (0)) {
$useripAddr 2. = $char;
}
}
Fclose ($FD);
Returns the city result for the IP address
if (Preg_match ('/http/i ', $useripAddr 2)) {
$useripAddr 2 = ";
}
$USERIPADDR = "$useripAddr 1 $useripAddr 2";
$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 enables city switching or jumping based on IP address
Here, in fact, the problem is very simple, with simple JS on all. Paragraph c is as follows:
Jump to specified page according to IP address JS get the city
Copy CodeThe code is as follows:
var city= ' ';

Jump to a specific page based on IP address for all cities
Copy CodeThe code is as follows:
if (City.indexof ("Shanghai City") >=0) {
Window.location.href= "http://shanghai.demo.com/";
}

Put the section a code at the beginning and the C code above in the head and tail of the B-segment code, and then we add the following code to the page where we need to jump:
Copy CodeThe code is as follows:


Refresh the page, is not to achieve the desired effect?
The above code is simply to judge the IP there on the jump to that, if so, I want to put all the IP in Hunan to jump to Changsha, that like Hengyang, Liu Yang, Leiyang these will be done many times the parent line processing, want to know please see the set.

http://www.bkjia.com/PHPjc/325883.html www.bkjia.com true http://www.bkjia.com/PHPjc/325883.html techarticle get the IP address copy code code as follows:? PHP function GetIP () {if ($_server["Http_x_forwarded_for"]) $ip = $_server["Http_x_forwarded_for" ]; else if ($_server["Http_client_ip"]) ...

  • 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.