PHP based on IP address to determine the city to achieve city switching or jump code _php tutorial

Source: Internet
Author: User
Tags get ip unpack
First to obtain the IP address is quite simple, the following first to obtain the IP address of the code, after the use of the QQIP library to find the current IP belongs to that IP segment and then the City field and return.

Get IP Address

The code is as follows Copy Code

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

The code is as follows Copy Code
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 detailed information, please see: http://www.bkjia.com/phper/php/33938.htm

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://8.zjdx2.crsky.com/201209/qqwry0830.rar

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:

The code is as follows Copy Code

/*
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

The code is as follows Copy Code

var city= ' ;

Jump to a specific page based on IP address for all cities

The code is as follows Copy Code

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:

The code is as follows Copy Code

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/444682.html www.bkjia.com true http://www.bkjia.com/PHPjc/444682.html techarticle first to obtain the IP address is quite simple, the following first to obtain the IP address of the code, after the use of the QQIP library to find the current IP belongs to that IP segment and then the City field and return. be ...

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