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

Comments

  1. User Avatar
    Swastik Solutions
    Permalink to comment#

    Much helpful in my next online directory project.
    Thanks.

  2. User Avatar
    CV
    Permalink to comment#

    very funky

  3. User Avatar
    sabithnarayanan

    Much helpful in my next online directory project.
    Thanks.

  4. User Avatar
    Karan
    Permalink to comment#

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

  5. User Avatar
    cnwtx
    Permalink to comment#

    Here’s an easier way:

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

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

  6. User Avatar
    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;

    }

    • User Avatar
      Satbir Singh
      Permalink to comment#

      Undermentioned error is :
      Notice: Undefined index: HTTP_X_FORWARDED_FOR in get-ip-address.php on line 19

  7. User Avatar
    CallTech
    Permalink to comment#

    That looks pretty straightforward and simple.
    Thanks.

  8. User Avatar
    adarsh
    Permalink to comment#

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

  9. User Avatar
    Starkprime
    Permalink to comment#

    where the IP ADDRESS will be stored?

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag