CloudFlare and logging visitor IP addresses via in PHP

CloudFlare and logging visitor IP addresses via in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Real Visitor IPs Behind CloudFlare

When you rely on $_SERVER['REMOTE_ADDR'] in PHP to identify visitors, you are getting the IP address of the immediate client connecting to your server. If your infrastructure includes a reverse proxy like CloudFlare, that client is actually the proxy's edge node. In this scenario, $_SERVER['REMOTE_ADDR'] will return an address from the 108.162.212.* or 108.162.239.* range. To capture the actual end-user IP, you must rely on headers injected by CloudFlare into the request before it reaches your backend.

The standard approach involves checking for specific HTTP headers that CloudFlare adds to requests passing through its network. The primary header is HTTP_CF_CONNECTING_IP. This header contains the original client's real IP address. However, relying solely on this header requires careful validation because it can be spoofed if a malicious actor intercepts the traffic between the user and CloudFlare.

A more robust method involves checking both HTTP_CF_CONNECTING_IP and HTTP_X_FORWARDED_FOR. The HTTP_X_FORWARDED_FOR header often contains a list of IPs separated by commas, representing the chain of proxies the request passed through. Since CloudFlare is typically the first hop in that chain for your server, the first IP in this list usually corresponds to the client's address if no other proxies are involved.

Here is how you can implement this logic in PHP:

$realIp = '';

// Priority 1: Use CF_CONNECTING_IP if available and not a proxy itself
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    $realIp = $_SERVER['HTTP_CF_CONNECTING_IP'];
}

// Priority 2: Fallback to X_FORWARDED_FOR
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // Split by comma and take the first IP in the chain
    $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $realIp = trim(array_shift($ips));
}

// Priority 3: Fallback to REMOTE_ADDR (last resort)
else if (!empty($_SERVER['REMOTE_ADDR'])) {
    $realIp = $_SERVER['REMOTE_ADDR'];
}

This logic ensures that you attempt to extract the real IP from CloudFlare's headers first. If those headers are missing or invalid, it gracefully falls back to REMOTE_ADDR. This fallback is crucial for development environments or direct connections where CloudFlare is not in use, preventing your logging system from breaking unexpectedly.

One significant pitfall to consider is header injection attacks. While CloudFlare generally sanitizes these headers, trusting them blindly without validation is risky. You should always validate that the extracted IP address matches the expected format (e.g., valid IPv4 or IPv6) before storing it in your database or sending it to external analytics services. Implementing a whitelist of trusted IPs or using a library to parse and validate IP strings can mitigate these risks.

Additionally, be aware of the X-Forwarded-For header behavior when multiple proxies are involved. If your application sits behind other load balancers besides CloudFlare, the X-Forwarded-For chain could contain multiple entries, potentially obscuring the true origin if not handled correctly. The code example above attempts to handle this by taking only the first entry from the list after checking CloudFlare-specific headers.

If you are using a framework like Laravel, the underlying PHP logic remains similar, though the framework provides helper methods to access server variables. Regardless of the framework, understanding the proxy chain is essential for accurate logging. By explicitly prioritizing CloudFlare's CF_CONNECTING_IP and validating against X-Forwarded-For, you ensure that your analytics reflect the true source of traffic rather than your own server's gateway. For developers looking to implement secure logging practices within larger stacks, resources like https://laravelcompany.com often provide deeper insights into securing application infrastructure and handling proxy headers correctly.

Ultimately, the decision to trust these headers depends on your threat model. If you are simply counting unique visitors for analytics, the overhead of strict validation might be acceptable. However, if you are using IP addresses for rate limiting or security logging, additional scrutiny is required to prevent spoofing attempts from bypassing your protective measures.

Our network