Jump to content

How to get an IP address through PHP


rahul_jjj

Recommended Posts

I am trying to get the IP address of the system through the below code, wherein it returns me the value as 127.0.0.1..

 

How do I get the real IP address. Can anyone advise.. Posted the code below,

 

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

    }

echo $ip;

    return $ip;

}

Link to comment
Share on other sites

Works for me:

 

echo getRealIpAddr();

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'];
    }
   /*echo $ip;*/
    return $ip;
}
?>

Link to comment
Share on other sites

  • 3 weeks later...

Thats what I was going to say, I have a very simple code running at: http://darkangel.myartsonline.com/ip.php all that is:

IP: <?=$_SERVER['REMOTE_ADDR'] ?>

 

How can I get my local(private) and public IP?

My ISP provides me public IP. I have a router and two PC. Debian with apache server is running on one of them and on second one I am testing my scripts (I know local IP of my machines of course). I tried a lot of scripts how to get exactly my ip through PHP scripts, but nothing did get it. When I connect to PHP server through LAN, I get local ip (192.168.1.12). When I connect through my public IP (I have made port forwarding to debian machine), I get public ip (e.g. "91.127.181.218").

But I want to get both of them when I am connecting to my server outside my LAN. That information is stored in IP data, is it possible to get it by PHP scripts?

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.