PHP: Get a visitor's real IP address.

Posted October 2nd, 2010


A more reliable way to obtain a visitor's IP address.

<?php
function getRealIp()
{
 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'];
 }
 return $ip;
}
?>