$ _ SERVER parameter HTTP_X_FORWARDED_FOR & amp; REMOTE_ADDR and get IP_PHP tutorial-php Tutorial

Source: Internet
Author: User
Tags get ip
$ _ SERVER parameter HTTP_X_FORWARDED_FOR & amp; REMOTE_ADDR and get IP address. 1. REMOTE_ADDR: IP address of the user's computer on the current page. HTTP_X_FORWARDED_FOR: view the gateway of the user's computer on the current page. HTTP_CLIENT_IP: the client ip address is $ _ 1 in PHP. REMOTE_ADDR: IP address of the user's computer on the current page. HTTP_X_FORWARDED_FOR: view the gateway of the user's computer on the current page. HTTP_CLIENT_IP: client ip address

Use $ _ SERVER ["REMOTE_ADDR"] in PHP to obtain the IP address of the client. However, if the client is accessed by a proxy SERVER, the IP address of the proxy SERVER is obtained, instead of the real client IP address. To obtain the real IP address of the client through the proxy SERVER, use $ _ SERVER ["HTTP_X_FORWARDED_FOR"] to read it.

However, not every proxy SERVER can use $ _ SERVER ["HTTP_X_FORWARDED_FOR"] to read the real IP address of the client, some of the IP addresses read by this method are still the proxy server IP addresses.

Note that if the client is not accessed through the proxy SERVER, the value obtained from $ _ SERVER ["HTTP_X_FORWARDED_FOR"] will be null. Therefore, if you want to use this method in the program, you can do this:

The code is as follows:

If ($ _ SERVER ["HTTP_X_FORWARDED_FOR"] = "")

{

$ User_ip = $ _ SERVER ["REMOTE_ADDR"];

}

Else

$ User_ip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];

?>

That is, if the client uses the proxy server, the value of HTTP_X_FORWARDED_FOR is obtained. if the client does not pass the proxy server, the value of REMOTE_ADDR is obtained.

Obtain the real IP address of the client

The code is as follows:

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 );

}


Differences between the three attributes for obtaining the user IP address (HTTP_X_FORWARDED_FOR, HTTP_VIA, REMOTE_ADDR)
I. no proxy server is used:

REMOTE_ADDR = your IP address
HTTP_VIA = no value or no Display
HTTP_X_FORWARDED_FOR = no value or no Display

II. Transparent Proxy Server: Transparent Proxies

REMOTE_ADDR = IP address of the last proxy server
HTTP_VIA = proxy server IP address
HTTP_X_FORWARDED_FOR = your real IP address. when multiple proxy servers are used, this value is similar to the following: 203.98.1820.3, 203.98.1820.3, 203.129.72.215.

This type of proxy server still forwards your information to your access object, which cannot hide your real identity.

III. normal Anonymous proxy server: Anonymous Proxies

REMOTE_ADDR = IP address of the last proxy server
HTTP_VIA = proxy server IP address
HTTP_X_FORWARDED_FOR = proxy server IP address. when multiple proxy servers are used, this value is similar to the following: 203.98.1820.3, 203.98.1820.3, 203.129.72.215.

Your real IP address is hidden, but you are disclosed to the access object that you use the proxy server to access them.

IV. destorting Proxies

REMOTE_ADDR = proxy server IP address
HTTP_VIA = proxy server IP address
HTTP_X_FORWARDED_FOR = random IP address. when multiple proxy servers are used, the value is as follows: 203.98.182.163, 203.98.182.163, 203.129.72.215.

It tells the access object that you used the proxy server, but fabricated a false random IP address instead of your real IP address to cheat it.

5. High Anonymity Proxies (Elite proxies)

REMOTE_ADDR = proxy server IP address
HTTP_VIA = no value or no Display
HTTP_X_FORWARDED_FOR = no value or no value is displayed. when multiple proxy servers are used, the value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

The proxy server information replaces all your information, just as you directly access the object using the proxy server.

The code is as follows:

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;
?>


Browse the gateway of the user's computer on the current page 3. HTTP_CLIENT_IP: use the client ip address 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.