This snippet will attempt to get a users IP address. The snippet uses various elements found in the PHP $_SERVER[] array.
Code:
<?php
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
?>
Note: Not all elements listed in $_SERVER[] are available on all servers.
Note: The use of proxies, VPN’s or other means of masking IP addresses may invalidate the IP address found by this snippet.
Example:
<?php echo "Your IP address is $ip"; ?>
Results:
Your IP address is 74.125.224.72 <!-- this is googles ip address -->
