Home > Backend Development > PHP Tutorial > Detailed introduction of PHP to obtain the real IP of the real client

Detailed introduction of PHP to obtain the real IP of the real client

黄舟
Release: 2023-03-06 12:34:01
Original
1367 people have browsed it

This article mainly introduces the method of PHP to obtain the real IP of the real client (REMOTE_ADDR, HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR), which has a good reference value. Let’s take a look with the editor below

REMOTE_ADDR is the IP when your client “handshakes” with your server. If an "anonymous proxy" is used, REMOTE_ADDR will display the IP of the proxy server.

HTTP_CLIENT_IP is the HTTP header sent by the proxy server. If it is a "super anonymous proxy", a value of none is returned. Likewise, REMOTE_ADDR will be replaced with the IP of this proxy server.

$_SERVER['REMOTE_ADDR']; //Accessor (may be a user, may be a proxy) IP

$_SERVER['HTTP_CLIENT_IP']; //Agent-side (may exist, can be forged)

##$_SERVER['HTTP_X_FORWARDED_FOR'] ; //Which IP the user is using as a proxy (may exist or can be forged)

The difference between the three values ​​is as follows:

1. When no proxy server is used:

REMOTE_ADDR = Your IP

HTTP_VIA = No value or no display

HTTP_X_FORWARDED_FOR = No value or not displayed

2. When using a transparent proxy server: Transparent Proxies

REMOTE_ADDR = The 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. When 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 the target audience 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. When using a high-anonymity proxy server: 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.

//获取用户IP
$ip = '';
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_FROM', 'REMOTE_ADDR') as $v) {
  if (isset($_SERVER[$v])) {
    if (! preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $_SERVER[$v])) {
        continue;
  } 
     $ip = $_SERVER[$v];
  }
}
uset($ip,$v);
Copy after login

The above is the detailed introduction of PHP to obtain the real IP of the real client. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template