Sentry Answers>PHP>

How to get the client IP address in PHP

How to get the client IP address in PHP

Nadia S.

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:

Click to Copy
// 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:

Click to Copy
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:

Click to Copy
// 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:

Click to Copy
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:

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

Output:

Click to Copy
No shared network IP address available
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark of Functional Software, Inc.