Skip to main content

PHP get real IP

✓ Published0🌍 Public
Aalessioalex
Last edited Nov 4, 2011
Created on Nov 4, 2011

This example shows a PHP function that determines a visitor’s real IP address by checking several common server variables. It first prioritizes `HTTP_CLIENT_IP` for shared internet connections, then falls back to `HTTP_X_FORWARDED_FOR` for proxy users, and finally uses `REMOTE_ADDR` if present and non-empty. The code relies on the PHP superglobal `$_SERVER` array to inspect these headers, and returns `127.0.0.1` as a localhost fallback when no valid IP is found. It demonstrates a straightforward conditional chain for server-side IP resolution without external libraries.

AI-generated description