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

Source: Internet
Author: User
Tags chr explode fread get ip php file pow strlen unpack

Get IP Address

The code is as follows Copy Code

<?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 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 details please see: http://www.111cn.net/phper/php/33938.htm

PHP to determine the user's city by IP address
the user's IP address has been obtained, and then we have 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, put the database below. QQ IP database is very convenient to use, the data is also very complete, you can timely attention to the official update to maintain the latest data, a strong recommendation:
Next, we in the above ipcity directory to create a new ipaddress.php file, directly copy the following code to go in, important place also made corresponding comments. Paragraph b:

The code is as follows Copy Code

?
/*
Function Name: ipCity
Parameter description: $userip--User IP address
Function: PHP to determine 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 to ' 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 result of the integer form is calculated
$userip = Explode ('. ', $userip);
$useripNum = $userip [0] * 16777216 + $userip [1] * 65536 + $userip [2] * 256 + $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 conversion growth shaping, if the data is negative then add 2 32 power
         $userip 1num = Implode (', Unpack (' L ', $useripData 1));
        if ($userip 1num < 0) $userip 1num + + POW (2);

The number of long integers fetched is greater than our IP address to modify the end position for the next loop
if ($userip 1num > $useripNum) {
$EndNum = $Middle;
Continue
}

Remove an index after fetching 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);
$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);

       //No IP address found for 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)) {
$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 of 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 to achieve city switching or jump based on IP address
Here, in fact, the problem has been very simple, with simple JS will be all done. Paragraph C reads as follows:
Jump the specified page based on the IP address JS get the city

The code is as follows Copy Code

var city= ' <?echo ipCity ($xp _userip);? > ';

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

The code is as follows Copy Code

if (City.indexof ("Shanghai City") >=0) {
Window.location.href= "http://shanghai.demo.com/";
}

Place the first section A and the C section of code on the head and tail of code B, and then we add the following code to the page where we want to jump:

The code is as follows Copy Code

<script src= "/ipcity/ipaddress.php" type= "Text/javascript" language= "JavaScript" ></script>

Refresh the page, is not to achieve the desired effect?

The above code is just simple to judge the IP there to jump to that, if there is such as, I want to the Hunan all IP jumped to Changsha, it is like Hengyang, irrepressible Liu Yang, Leiyang these will be done many times the parent line processing, want to know, please see the episode.

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.