Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:

Get Users IP Address

Accounts for proxies:

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'];
}

Reference URL

Comments

  1. Swastik Solutions
    Permalink to comment#

    Much helpful in my next online directory project.
    Thanks.

  2. CV
    Permalink to comment#

    very funky

  3. sabithnarayanan

    Much helpful in my next online directory project.
    Thanks.

  4. Karan
    Permalink to comment#

    Thanks! I’ve been looking for a PHP IP code for ages!

  5. cnwtx
    Permalink to comment#

    Here’s an easier way:

    echo getenv('REMOTE_ADDR'); 
    • Avinash
      Permalink to comment#

      Its easier but it will not give proper IP address when your user is behind proxy…

  6. Robert Queen
    Permalink to comment#

    This more complete, as I use in one of my site ..

    function getRealIP()
    {

    if( $_SERVER[‘HTTP_X_FORWARDED_FOR’] != ” )
    {
    $client_ip =
    ( !empty($_SERVER[‘REMOTE_ADDR’]) ) ?
    $_SERVER[‘REMOTE_ADDR’]
    :
    ( ( !empty($_ENV[‘REMOTE_ADDR’]) ) ?
    $_ENV[‘REMOTE_ADDR’]
    :
    “unknown” );

    // Proxies are added at the end of this header
    // Ip addresses that are “hiding”. To locate the actual IP
    // User begins to look for the beginning to find
    // Ip address range that is not private. If not
    // Found none is taken as the value REMOTE_ADDR

    $entries = split(‘[, ]’, $_SERVER[‘HTTP_X_FORWARDED_FOR’]);

    reset($entries);
    while (list(, $entry) = each($entries))
    {
    $entry = trim($entry);
    if ( preg_match(“/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/”, $entry, $ip_list) )
    {
    // http://www.faqs.org/rfcs/rfc1918.html
    $private_ip = array(
    ‘/^0\./’,
    ‘/^127\.0\.0\.1/’,
    ‘/^192\.168\..*/’,
    ‘/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/’,
    ‘/^10\..*/’);

    $found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);

    if ($client_ip != $found_ip)
    {
    $client_ip = $found_ip;
    break;
    }
    }
    }
    }
    else
    {
    $client_ip =
    ( !empty($_SERVER[‘REMOTE_ADDR’]) ) ?
    $_SERVER[‘REMOTE_ADDR’]
    :
    ( ( !empty($_ENV[‘REMOTE_ADDR’]) ) ?
    $_ENV[‘REMOTE_ADDR’]
    :
    “unknown” );
    }

    return $client_ip;

    }

  7. CallTech
    Permalink to comment#

    That looks pretty straightforward and simple.
    Thanks.

  8. adarsh
    Permalink to comment#

    not working getting “::1″ as output :-(

  9. Starkprime
    Permalink to comment#

    where the IP ADDRESS will be stored?

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

```
<script>
  function example() {
    element.innerHTML = "<div>code</div>";
  }
</script>
```