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
https://forums.phpfreaks.com/topic/138069-how-to-get-an-ip-address-through-php/
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;
}
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.