$_SERVER["REMOTE_ADDR"] gives server IP rather than visitor IP

$_SERVER["REMOTE_ADDR"] gives server IP rather than visitor IP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

$_SERVER["REMOTE_ADDR"] gives server IP rather than visitor IP

When you encounter a scenario where $_SERVER['REMOTE_ADDR'] returns your own server's IP address instead of the client's, it is almost never a misconfiguration of PHP or a quirk of the operating system. The variable itself functions exactly as documented: it contains the remote host that initiated the TCP connection to your application. If you are seeing your server's IP there, the issue lies upstream in the network path between the user and your web server.

The most common cause is an intermediary proxy or load balancer. In high-availability architectures, users typically do not connect directly to the origin web server. Instead, they hit a CDN (like Cloudflare) or a reverse proxy (like Nginx or HAProxy). These intermediaries terminate the initial client connection and establish a new internal connection to your PHP application. Because of this handoff, $_SERVER['REMOTE_ADDR'] reflects the IP of the intermediary server, not the original visitor.

To verify this hypothesis, check if the variable remains constant regardless of where you test from. If every user reports the same IP address for their request, or if that IP belongs to your data center rather than a residential ISP, an intermediate layer is definitely stripping or replacing the client information. This behavior is standard practice in production environments to protect the origin server's internal network topology and to manage traffic efficiently.

The solution depends entirely on whether your infrastructure supports passing the original header through. The industry-standard method involves inspecting specific HTTP headers injected by the proxy. For Nginx, Apache with mod_proxy, or Cloudflare, the standard header is HTTP_X_FORWARDED_FOR. When a proxy receives a request from a user at 203.0.113.45 and forwards it to your backend at 198.51.100.2, the proxy adds X-Forwarded-For: 203.0.113.45 to the request headers before forwarding it. Your PHP application should read this value instead of relying on REMOTE_ADDR.

$ip = $_SERVER['REMOTE_ADDR'];

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // X-Forwarded-For can contain multiple IPs if requests are proxied multiple times.
    // The first IP in the list is typically the original client.
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

However, simply trusting X-Forwarded-For without validation introduces security risks. A malicious user can modify the request headers if your application does not trust them implicitly. If you are using a trusted CDN or proxy that you control, you can safely prioritize X-Forwarded-For. If you are dealing with untrusted external traffic, you must validate that the header matches the actual connection source before accepting it as authentic.

Another potential pitfall involves the presence of multiple IPs in the X-Forwarded-For header string. When a request passes through multiple proxies (e.g., User -> CDN -> Load Balancer -> Origin), the header might look like 203.0.113.45, 198.51.100.2, 192.0.2.1. In this case, the first IP is the client, and the subsequent ones are the intermediate hops. You must ensure your parsing logic extracts only the first entry if you intend to identify the original visitor.

If your application framework, such as Laravel, handles these headers automatically, it often provides helper methods or middleware to resolve the true client IP, abstracting away the complexity of $_SERVER variables. Always check the framework's documentation for built-in utilities designed for this specific problem before writing custom parsing logic.

Finally, if you are unable to configure your proxies to send the correct headers, or if you need to rely solely on the TCP connection metadata, there are no magic PHP settings that will override the network reality. The only way to get the true client IP without relying on headers is to ensure every request reaches your origin server directly, which defeats the purpose of using a proxy layer for security and performance. In most enterprise scenarios, trusting the configured proxy headers is the correct architectural decision, provided you implement strict validation rules.

Our network