# PHP Get Client IP Address from $_SERVER Variables

> Access the client IP address in PHP using $_SERVER['REMOTE_ADDR'], handle proxy forwarding with HTTP_X_FORWARDED_FOR, and detect shared network IPs

**URL:** https://sentry.io/answers/how-to-get-the-client-ip-address-in-php/

---

## The Problem

You have a PHP application and want to keep track of visitors to analyze your traffic or for customization or security purposes. How can you do this?

## The Solution

You can use the `$_SERVER` superglobal variable with the`'REMOTE_ADDR'` key to get the IP address making the request to your application's server. For example:

```php
// get the immediate IP address
echo 'Client IP making the request: ' . $_SERVER['REMOTE_ADDR'];
```

As we are running a local server, the IP address making the request is `::1`, which is the same as the IPv4 `127.0.0.1` localhost IP address. For example:

```
Client IP making the request: ::1
```

It's crucial to remember that client IP addresses are easy to manipulate. There's no guarantee that any IP addresses you get using this method are accurate. However, the information is still valuable for analyzing traffic to your application.

### Proxy servers and load balancers

If a request to your application comes via a proxy server (like a VPN) or load balancer, the IP address making the request will differ from the original IP address.

You can use the `$_SERVER` superglobal variable with the `'HTTP_X_FORWARDED_FOR'` keys to get the IP addresses of the last proxy and the original requestor.

To prevent an `Undefined array key` error, we use an if statement and first check that the `'HTTP_X_FORWARDED_FOR'` value is present before echoing it out. For example:

```php
// get the original IP address
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    echo 'Forwarding IP: ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    echo 'No forwarding IP address(es) available';
}
```

If the request does not come from a proxy server, the output is:

```
No forwarding IP address(es) available
```

Note that, even if there is no value for the `$_SERVER['HTTP_X_FORWARDED_FOR']` expression, the request might still have come from a proxy server or load balancer. Certain configurations and network settings could omit or strip information about the forwarding IP addresses. There could also be numerous forwarding IP addresses, in which case the `'HTTP_X_FORWARDED_FOR'` value will contain several comma-separated IP addresses.

It's a good idea to record both the most recent and original client IP addresses to increase your chances of uniquely identifying a visitor to your application or website.

### Network sharing

You can use the `$_SERVER['HTTP_CLIENT_IP']` variable to attempt to get the IP address of a shared network. However, this shared network information is not widely used in server configurations.

We use an if statement to check whether the value is present before attempting to echo it out. For example:

```php
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  echo 'Forwarded IP: ' . $_SERVER['HTTP_CLIENT_IP'];
} else {
    echo 'No shared network IP address available';
}
```

Output:
```
No shared network IP address available
```

---

*Source: [sentry.io/answers/how-to-get-the-client-ip-address-in-php/](https://sentry.io/answers/how-to-get-the-client-ip-address-in-php/)*
