Getting real IP address in PHP

Are you using $_SERVER[‘REMOTE_ADDR’] to find the the client’s IP address in PHP? Well dude, you might be amazed to know that it may not return the true IP address of the client at all time. If your client is connected to the Internet through Proxy Server then $_SERVER[‘REMOTE_ADDR’] in PHP just returns the the IP address of the proxy server not of the client’s machine. So here is a simple function in PHP to find the real IP address of the client’s machine. There are extra Server variable which might be available to determine the exact IP address of the client’s machine in PHP, they are HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR.

Function to find real IP address in PHP

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

In this PHP function, first attempt is to get the direct IP address of client’s machine, if not available then try for forwarded for IP address using HTTP_X_FORWARDED_FOR. And if this is also not available, then finally get the IP address using REMOTE_ADDR.

79 thoughts on “Getting real IP address in PHP

  1. Vinayak

    Thanks Dude! You helped alot! Keep rockingggggg

  2. Hey!…Thanks for the nice read, keep up the interesting posts..what a nice Saturday

  3. Brilliant – this will work 98% of the time – and if someone is trying to fake there IP address I don’t care.

  4. “is there a way in php of knowing if the email address supplied by the user is existing?”

    Yes, take a look at this PHP library – http://code.google.com/p/php-smtp-email-validation/
    It validates email addresses through SMTP.

  5. I did the following, however, please note that I required the VPN detection and simply return VPN for local ips.

    function getRealIP($fakeip=false) {

    $ip = (!empty($_SERVER[‘HTTP_CLIENT_IP’])) ? (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) ? $_SERVER[‘HTTP_CLIENT_IP’] : preg_replace(‘/(?:,.*)/’, ”, $_SERVER[‘HTTP_X_FORWARDED_FOR’]):$_SERVER[‘REMOTE_ADDR’];
    $ip = (!$fakeip) ? $ip:$fakeip;

    // local check class b and c
    $patterns = array(“/(192).(168).(\d+).(\d+)/i”,”/(10).(\d+).(\d+).(\d+)/i”);
    foreach($patterns as $pattern) {
    if(preg_match($pattern,$ip)) {
    return “VPN”;
    }
    }
    // local check class a
    $parts = explode(“.”,$ip);
    if($parts[0]==172 && ($parts[1]>15 || $parts[1]<32)) {
    return "VPN";
    }
    return trim($ip);
    }

  6. Travis

    Ben: You’re an idiot, this script gets the physical location IP address for geostatistics. The person’s private IP (192.168.0.x) means absolutely nothing to a web page owner.

  7. Thanks, very simple, fast and right way to get client`s ip adress. Got to use it in my scripts.

  8. thanks.. used this in my webpage. its worked for me.

  9. Ben

    Actually your an idiot because if you have the public IP it licenses the entire site! So you must write really stupid code! your an idiot for not even knowing why someone would want the local IP! So you don’t really write applications you just work with web sites! So shut up idiot and continue to to write you TRASH code!

  10. Ben

    That was for you TRAVIS! Someone told you it was useless so you repeated it! D@MN you are STUPID!

  11. Ben

    Yes, I see you say WEB PAGE OWNER and since you don’t need to license the WEBSITE then it is no big deal! You must be ENTRY LEVEL!!!!

  12. Ben

    TRAVIS is the kind of person that uses TEMPLATE MONSTER to do most of his work so that is why he would NEVER have to license something! IF ANYONE LISTENS TO TRAVIS YOU WILL FAIL HE IS AN DUMB A$$!

    Explain to us TRAVIS how you license you PHP web applications? You don’t and you might script a little, but you are NOT a WEB APPLICATION DEVELOPER! I DO COLLECT THE PUBLIC IP AND THEN ALL THE LOCAL IP ADDRESSES AND MANAGE THE NUMBER OF TIMES A USER LOGS INTO MY APP USING A COMBINATION OF LOCAL IP, MAC ADDRESS AND USER INFO! MAN YOU ARE A DUMB A$$!

  13. Hi Rohan,

    I got the real IP address, but i need Router IP Address (LAN IP) which is like http://www.find-ip-address.org/ , can you please suggest how to get the code using php code.

    Thank you in advance.

  14. @AnilKumarPanigrahi See they are using JAVA for it, download the class http://www.find-ip-address.org/MyAddress.class and use it as they are using. Its simple

  15. json

    Ben, calm down, this is a topic about PHP and ip addresses, not MAC addresses. If you are coding licensed works using PHP you are indeed an even bigger arse hole! You rant how you keep track of IP addresses, both public and private combinations in order to keep track of licensing…. don’t make me laugh! If that’s your licensing security solution it’s dumb in more ways than I can begin to break down. MAC codes I can see but IP, laughable!!!! Flame me all you like though kid as you obviously know more than someone with as much experienced as me 😉 Commercial developer with PHP/Mysql/Linux/Apache since 1996.

Comments are closed.