Home >Backend Development >PHP Tutorial >$_SERVER parameters HTTP_X_FORWARDED_FOR & REMOTE_ADDR and getting IP_PHP tutorial

$_SERVER parameters HTTP_X_FORWARDED_FOR & REMOTE_ADDR and getting IP_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:11:121106browse

1.REMOTE_ADDR: The IP address of the user's computer browsing the current page 2.HTTP_X_FORWARDED_FOR: The gateway of the user's computer browsing the current page 3.HTTP_CLIENT_IP: The client's IP

Use $_SERVER["REMOTE_ADDR"] in PHP to obtain the client's IP address IP address, but if the client uses a proxy server to access, what is obtained is the IP address of the proxy server, not the real client IP address. To obtain the client's real IP address through a proxy server, use $_SERVER["HTTP_X_FORWARDED_FOR"] to read it.

However, something to note is that not every proxy server can use $_SERVER["HTTP_X_FORWARDED_FOR"] to read the real IP of the client. Some of the IPs read by this method are still the IP of the proxy server. .

Another thing to note is that if the client does not access through a proxy server, the value obtained with $_SERVER["HTTP_X_FORWARDED_FOR"] will be empty. Therefore, if you want to use this method in your program, you can do it like 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"];

?>

if ($_SERVER["HTTP_X_FORWARDED_FOR"]==”")

{
代码如下 复制代码

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

}

<🎜>$user_ip=$_SERVER["REMOTE_ADDR"];<🎜><🎜>}<🎜><🎜>else<🎜><🎜>$user_ip=$_SERVER[" HTTP_X_FORWARDED_FOR"];<🎜><🎜>?>
That is: if the client passes through the proxy server, the value of HTTP_X_FORWARDED_FOR is taken, if it does not pass through the proxy server, take the value of REMOTE_ADDR. Get the real IP address of the client
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);}


The difference between the three attributes of obtaining the user's IP address (HTTP_X_FORWARDED_FOR, HTTP_VIA, REMOTE_ADDR)
1. Without using a proxy server:

REMOTE_ADDR = your IP
HTTP_VIA = No value or not displayed
HTTP_X_FORWARDED_FOR = No value or not displayed

2. When using transparent proxy servers: Transparent Proxies

REMOTE_ADDR = Last proxy server IP
HTTP_VIA = Proxy server IP
HTTP_X_FORWARDED_FOR = Your real IP. When passing through multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

This type of proxy server still forwards your information to the person you visit, and cannot achieve the purpose of hiding your true identity.

3. The situation of using ordinary anonymous proxy servers: Anonymous Proxies

REMOTE_ADDR = the last proxy server IP
HTTP_VIA = proxy server IP
HTTP_X_FORWARDED_FOR = proxy server IP, after multiple When using a proxy server, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Hide your real IP, but reveal to your visitors that you are using a proxy server to access them.

4. The use of deceptive proxy servers: Distorting Proxies

REMOTE_ADDR = proxy server IP
HTTP_VIA = proxy server IP
HTTP_X_FORWARDED_FOR = random IP, through multiple proxies When using the server, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Tell the visitor that you are using a proxy server, but make up a fake random IP instead of your real IP to trick it.

5. When using high-anonymity proxy servers: High Anonymity Proxies (Elite proxies)

REMOTE_ADDR = Proxy server IP
HTTP_VIA = No value or not displayed
HTTP_X_FORWARDED_FOR = None The value may not be displayed. When passing through multiple proxy servers, the value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Completely replaces all your information with the proxy server's information, just like you are using that proxy server to directly access the object.

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 "你的IP:".$ip ;
?>

Copy code
if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])

{

$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];

} { $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/444656.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444656.htmlTechArticle1.REMOTE_ADDR: The IP address of the user’s computer browsing the current page 2.HTTP_X_FORWARDED_FOR: The IP address of the user’s computer browsing the current page Gateway 3. HTTP_CLIENT_IP: The client’s ip is used in PHP $_...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP memory cache Memcached class code_PHP tutorialNext article:PHP memory cache Memcached class code_PHP tutorial

Related articles

See more