You are here:Home > Best VPN > In depth analysis of several PHP methods for obtaining client IP addresses

In depth analysis of several PHP methods for obtaining client IP addresses

Summary:In this article, we will provide a detailed introduction for everyoneIn PHP, the client IP address is often obtained using $_SRVER ["REMOTE-ADDR&…
horizonVPN Official website sandVPN Official website VPNhood Official website hailianVPN Official website
GoduckVPN Official website Fast Wind VPN Official website Fast Orange VPN Official website(Recommend) NetShekanVPN Official website

This book is available as an e-book in EPUB format

PayPal sponsors this website for any amount. I will send you an EPUB e-book via email. Please send me an email

Email: shuxueha@outlook.com

PayPal: https://paypal.me/zhipinglei

You can also scan the PayPal QR code below to sponsor

You can also purchase this book on Amazon: click to enter the Amazon official website

In this article, we will provide a detailed introduction for everyone

In PHP, the client IP address is often obtained using $_SRVER ["REMOTE-ADDR"]. But if the client uses a proxy server to access, the IP address obtained is the proxy server's IP address, not the real client's IP address. To obtain the client's real IP address through a proxy server, you need to use $_SRVER ["HTTP_X_FORWARDED_FOR"] to read it.

But only when the client uses a "transparent proxy", the value of $ESERVER ["HTTP_X_FORWARDED_FOR"] is the client's true IP (if it is a multi-layer proxy, this value may be composed of the client's true IP and the IP of multiple proxy servers, separated by commas ","), while in the case of "anonymous proxy" and "deceptive proxy", it is the IP value of the proxy server (if it is a multi-layer proxy, this value may be composed of the IP of multiple proxy servers, separated by commas ","), and in the case of "high anonymous proxy", it is an empty value.

We will provide a detailed introduction to the REMOTE-ADDR and HTTP-FORWARED-FOR values in HTTP header information in the following text. Assuming that the client's real IP is 221.5.252.160:

1、 PHP without using a proxy server to obtain client IP:

Remote Address=Client IP

HTTP_X_FORWARDED_FOR=No value or not displayed

2、 When using transparent proxy servers: Transparent Proxies

Remote Address=Last Proxy Server IP

HTTP_X_FORWARDED_FOR=Client Real IP (When passing through multiple proxy servers, this value is similar to: 221.5.252.160, 203.98.182.163, 203.129.72.215)

This type of proxy server still sends the client's real IP to the accessing object, which cannot achieve the purpose of hiding the real identity.

3、 Using PHP from a regular anonymous proxy server to obtain client IP information: Anonymous Proxies

Remote Address=Last Proxy Server IP

HTTP_X_FORWARDED_FOR=Proxy Server IP (When passing through multiple proxy servers, this value is similar to: 203.98.182.163, 203.98.182.163, 203.129.72.215)

In this case, the client's real IP is hidden, but it is revealed to the accessing object that the client is using a proxy server to access them.

4、 The use of deceptive proxy servers: Distorting Proxies

Remote Address=Proxy Server IP

HTTP_X_FORWARDED_FOR=Random IP (When passing through multiple proxy servers, this value is similar to: 220.4.251.159, 203.98.182.163, 203.129.72.215)

In this case, it was also revealed that the client was using a proxy server, but fabricated a false random IP address (220.4.251.159) instead of the client's real IP to deceive it.

5、 Using PHP with a high anonymity proxy server to obtain client IP information: High Anonymity Proxies (Elite Proxies)

Remote Address=Proxy Server IP

HTTP_X_FORWARDED_FOR=No value or not displayed

Whether it's REMOTE-ADDR or HTTP-FORWARDED_FOR, these header messages may not be obtainable because different browsers and network devices may send different IP header messages. Therefore, the values obtained by PHP using $_SRVER ["REMOTE-ADDR"] and $_SRVER ["HTTP_X_FORWARDED_FOR"] may be null or "unknown" values.

Therefore, the code to obtain the client IP using PHP can be as follows:

function getip() {

$unknown = ‘unknown’;

if ( isset($_SERVER[‘HTTP_X_FORWARDED_FOR’])

&& $_SERVER[‘HTTP_X_FORWARDED_FOR’]

&& strcasecmp($_SERVER[‘HTTP_X_FORWARDED_FOR’],

$unknown) ) {

$ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];

} elseif ( isset($_SERVER[‘REMOTE_ADDR’])

&& $_SERVER[‘REMOTE_ADDR’] &&

strcasecmp($_SERVER[‘REMOTE_ADDR’], $unknown) ) {

$ip = $_SERVER[‘REMOTE_ADDR’];

}

/*

Dealing with situations involving multiple layers of agents

Or use the regularization method: $ip=preg-match ("/[d.]

{7,15}/”, $ip, $matches) ?  $matches[0] : $unknown;

*/

if (false !== strpos($ip, ‘,’))

$ip = reset(explode(‘,’, $ip));

return $ip;

}

Another thing to note when obtaining the client IP in PHP is that using the functions getenv ('HTTP_X_FORWARDED_FOR ') or getenv ('REMOTE-ADR') can also achieve the same effect as the above code. But getenv() does not support PHP running in IIS' isapi mode.


High-speed VPN Fast VPN Preferred Accelerator VPN Reliable VPN
High quality VPN Old fashioned VPN Stable VPN(Recommend) Smooth VPN
Share with Friends: