0

I want to get the IPv4 address, but not the localhost address (127.0.0.1). I just get ::1.

i tried it with $ip = getenv ("REMOTE_ADDR");

and

$ip = getenv ('SERVER_ADDR');
Valcone
  • 59
  • 2
  • 15
  • Possible duplicate of [How to get the client IP address in PHP?](http://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php) – samlev Nov 12 '15 at 15:01

3 Answers3

1
$ip = $_SERVER['REMOTE_ADDR'];
fico7489
  • 7,931
  • 7
  • 55
  • 89
0

Finally i got the answer at the same day. You can see it below:

    function getIP() {
    $ip = $_SERVER['SERVER_ADDR'];

    if (PHP_OS == 'WINNT'){
        $ip = getHostByName(getHostName());
    }

    if (PHP_OS == 'Linux'){
        $command="/sbin/ifconfig";
        exec($command, $output);
        // var_dump($output);
        $pattern = '/inet addr:?([^ ]+)/';

        $ip = array();
        foreach ($output as $key => $subject) {
            $result = preg_match_all($pattern, $subject, $subpattern);
            if ($result == 1) {
                if ($subpattern[1][0] != "127.0.0.1")
                $ip = $subpattern[1][0];
            }
        //var_dump($subpattern);
        }
    }
    return $ip;
}
Valcone
  • 59
  • 2
  • 15
0

You get the IPv4 address with gethostbyname(), for example with gethostbyname($_SERVER['HTTP_HOST']).

apaderno
  • 28,547
  • 16
  • 75
  • 90