How can I get the user's IP address in PHP?
<h3>Duplicate:</h3>

<blockquote>
  <p><a href="http://stackoverflow.com/questions/55768/how-do-i-find-a-users-ip-address-with-php">How do I find a user's IP address with php?</a></p>
</blockquote>

<p>Edit: Server is a Debian linux box running PHP5 through suPHP. Above post has been read. This code covers all points mentioned but still returns unknown addresses.</p>

<p>My code always requires that the remote IP address be known. It doesn't matter if we pick up the proxy address once we can get some IP address for the access.</p>

<p>Function below is what we current use however in over 20% of kits, the server falls through to the unknown case and has nothing in the $_SERVER var.</p>

<pre><code>function getip()
{
    if ( $_SERVER["HTTP_CLIENT_IP"] &amp;&amp; strcasecmp($_SERVER["HTTP_CLIENT_IP"], "unknown") )
    {
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    else if ( $_SERVER["HTTP_X_FORWARDED_FOR"] &amp;&amp; strcasecmp($_SERVER["HTTP_X_FORWARDED_FOR"], "unknown") )
    {
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    else if (getenv("REMOTE_ADDR") &amp;&amp; strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
    {
        $ip = getenv("REMOTE_ADDR");
    }
    else if (isset($_SERVER['REMOTE_ADDR']) &amp;&amp; $_SERVER['REMOTE_ADDR'] &amp;&amp; strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    else
    {
        $ip = "unknown: ".var_dump($_SERVER, true);
    }
    return($ip);
}
</code></pre>
