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. Much helpful in my next online directory project.
    Thanks.

  2. Permalink to comment#

    very funky

  3. sabithnarayanan

    Much helpful in my next online directory project.
    Thanks.

  4. 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'); 
  6. 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. Permalink to comment#

    That looks pretty straightforward and simple.
    Thanks.

  8. adarsh
    Permalink to comment#

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

  9. where the IP ADDRESS will be stored?

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".