Get Real IP Address with PHP

DZone 's Guide to

Get Real IP Address with PHP

· ·
Free Resource

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}