Get real IP host address with php
Getting one's real IP address is a bit more difficult, as users can 'hide' behind proxy or sharing their IP's.
Here's how you would usualy get the IP Address from your visitor(s).
$ip=$_SERVER['REMOTE_ADDR'];
The following function named getIpAddress first checks if the visitor is using a shared internet IP. If the visitor is not using this, it will verify the visitor is hiding not behind a proxyserver.
Once both checks have been negative, the function will take the regular approach to getting one's IP address. By using $ip=$_SERVER['REMOTE_ADDR'].
function getIpAddress()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
Please note that the explained function does not always guarantee getting the correct IP Address.
There are methods of bypassing the function, for example using IP tunnels and malicious remote management tools.
Posted by James on 2009-07-10 in the category " php "
