$_server parameters Http_x_forwarded_for & remote_addr and get ip_php tutorial

Source: Internet
Author: User
1.REMOTE_ADDR: Browse the current page of the user's computer's IP address 2. Http_x_forwarded_for: Browse the current page of the user's computer Gateway 3. HTTP_CLIENT_IP: IP for Client

Use $_server["REMOTE_ADDR" in PHP to obtain the IP address of the client, but if the client is accessed using a proxy server, it is the IP address of the proxy server, not the real client IP address. To get the real IP address of the client through the proxy server, it is necessary to use $_server["http_x_forwarded_for" to read.

However, it is important to note that not every proxy server can use $_server["http_x_forwarded_for" to read the client's real IP, some of which are still read by this method is the proxy server IP.

It is also important to note that if the client is not accessed through a proxy server, then the value taken with $_server["http_x_forwarded_for" will be empty. Therefore, if you want to use this method in your program, you can do this:

The code is as follows Copy Code

if ($_server["http_x_forwarded_for"]== "")

{

$user _ip=$_server["REMOTE_ADDR"];

}

Else

$user _ip=$_server["Http_x_forwarded_for"];

?>

That is: If the client through the proxy server, then take the value of http_x_forwarded_for, if not through the proxy server, take the value of REMOTE_ADDR.

Get the client's real IP address

The code is as follows Copy Code

function GetIP () {

if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "Unknown")

$ip = getenv ("Http_client_ip");

else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown")

$ip = getenv ("Http_x_forwarded_for");

else if (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "Unknown")

$ip = getenv ("REMOTE_ADDR");

else if (isset ($_server[' remote_addr ')) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], "unknown"))

$ip = $_server[' remote_addr ');

Else

$ip = "Unknown";

return ($IP);

}


Get the difference between the three attributes of a user's IP address (http_x_forwarded_for,http_via,remote_addr)
One, not using the proxy server situation:

REMOTE_ADDR = Your IP
Http_via = no value or no display
Http_x_forwarded_for = no value or no display

Second, the use of transparent proxy server situation: Transparent Proxies

REMOTE_ADDR = Last Proxy server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Your real IP, after multiple proxy servers, this value resembles the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

This type of proxy server also forwards your information to your Access object, and does not achieve the purpose of hiding the true identity.

Third, the use of ordinary anonymous proxy server situation: Anonymous Proxies

REMOTE_ADDR = Last Proxy server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Proxy IP, after multiple proxies, this value resembles the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Hides your real IP, but reveals to the Access object that you are using a proxy server to access them.

Iv. use of deceptive proxy servers: distorting Proxies

REMOTE_ADDR = Proxy Server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Random IP, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Tells the Access object that you used a proxy server, but fabricated a bogus random IP to spoof it instead of your real IP.

V. Use of highly anonymous proxy servers: High anonymity Proxies (Elite Proxies)

REMOTE_ADDR = Proxy Server IP
Http_via = no value or no display
Http_x_forwarded_for = no value or not displayed, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Full use of Proxy server information replaces all of your information, as if you were using the proxy server directly to access the object.

The code is as follows Copy Code

if ($HTTP _server_vars["Http_x_forwarded_for"])
{
$ip = $HTTP _server_vars["Http_x_forwarded_for"];
}
ElseIf ($HTTP _server_vars["HTTP_CLIENT_IP"])
{
$ip = $HTTP _server_vars["Http_client_ip"];
}
ElseIf ($HTTP _server_vars["REMOTE_ADDR"])
{
$ip = $HTTP _server_vars["REMOTE_ADDR"];
}
ElseIf (getenv ("Http_x_forwarded_for"))
{
$ip = getenv ("Http_x_forwarded_for");
}
ElseIf (getenv ("Http_client_ip"))
{
$ip = getenv ("Http_client_ip");
}
ElseIf (getenv ("REMOTE_ADDR"))
{
$ip = getenv ("REMOTE_ADDR");
}
Else
{
$ip = "Unknown";
}
echo "Your IP:". $ip;
?>

http://www.bkjia.com/PHPjc/629983.html www.bkjia.com true http://www.bkjia.com/PHPjc/629983.html techarticle 1.remote_addr: Browse the current page of the user's computer's IP address 2. Http_x_forwarded_for: Browse the current page of the user's computer Gateway 3. HTTP_CLIENT_IP: Client IP is used in PHP $_ ...

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