Code Snippet

Home » Code Snippets » PHP » Get Users IP Address

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

Subscribe to The Thread

  1. Much helpful in my next online directory project.
    Thanks.

  2. 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

    Here’s an easier way:

    echo getenv('REMOTE_ADDR'); 
    • Its easier but it will not give proper IP address when your user is behind proxy…

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~