DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Avinash has posted 1 posts at DZone. View Full User Profile

Get Real IP Address with PHP

  • submit to reddit
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;

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.