Over a million developers have joined DZone.

Get Real IP Address with PHP


In php, $_SERVER['REMOTE_ADDR'] is used to get the IP address of the user. But what happen if any user from USA access you site via proxy server of Australia. In this case $_SERVER['REMOTE_ADDR'] will return ip address of the Australia rather than ip address of USA.

This function will be useful when you have such visitors who are actually behind the proxy.


if (!empty($_SERVER["HTTP_CLIENT_IP"]))
//check for ip from share internet
elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
// Check for the Proxy User
// This will print user's real IP Address
// does't matter if user using proxy or not.
echo $ip;

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}