Home > Backend Development > PHP Tutorial > php HTTP_X_FORWARDED_FOR, HTTP_VIA, REMOTE_ADDR Difference Summary_PHP Tutorial

php HTTP_X_FORWARDED_FOR, HTTP_VIA, REMOTE_ADDR Difference Summary_PHP Tutorial

WBOY
Release: 2016-07-13 10:44:23
Original
1130 people have browsed it

This article introduces a summary of the differences between HTTP_X_FORWARDED_FOR, HTTP_VIA, and REMOTE_ADDR in php when obtaining the user's real IP address.

1. The case of not 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. The use of 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 your visitor, which cannot achieve the purpose of hiding your true identity.

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

REMOTE_ADDR = Last proxy server IP
HTTP_VIA = Proxy server IP
HTTP_X_FORWARDED_FOR = Proxy server 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.

Hides your real IP, but reveals 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. 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.

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. The use of 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 = No value or not displayed. 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.

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

Example 1 (Get the user’s real IP address)

The code for HP to obtain the user’s real IP address is as follows

The code is as follows Copy code
 代码如下 复制代码

 function getIp() {

  if($_SERVER['HTTP_CLIENT_IP']) {

  $ip = $_SERVER['HTTP_CLIENT_IP'];    //PHP获取IP

  } elseif ($_SERVER['HTTP_X_FORWARDED_FOR']) {

  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

  } else {

  $ip = $_SERVER['REMOTE_ADDR'];    //PHP获取IP

  }

  }

function getIp() {  if($_SERVER['HTTP_CLIENT_IP']) { $ip = $_SERVER['HTTP_CLIENT_IP']; //PHP gets IP  } elseif ($_SERVER['HTTP_X_FORWARDED_FOR']) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  } else { $ip = $_SERVER['REMOTE_ADDR']; //PHP gets IP  }  }


例子2

 代码如下
 代码如下 复制代码

/*

mktime:2012/9/9

*/
//获取用户真实IP
function get_client_ip() {
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);
}
echo $ip=get_client_ip();//这就是你的ip地址
?>

复制代码

/*

*/ //获取用户真实IP function get_client_ip() { 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);<🎜> }<🎜> echo $ip=get_client_ip();//这就是你的ip地址<🎜> ?> http://www.bkjia.com/PHPjc/633104.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633104.htmlTechArticle本文章介绍了在获取用户真实IP地址时php中的HTTP_X_FORWARDED_FOR,HTTP_VIA,REMOTE_ADDR 区别总结. 一、没有使用代理服务器的情况: REMOTE_ADDR = 您的...
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template