Reputation: 55
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');
Upvotes: 0
Views: 5290
Reputation: 1
You get the IPv4 address with gethostbyname()
, for example with gethostbyname($_SERVER['HTTP_HOST'])
.
Upvotes: 0
Reputation: 55
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;
}
Upvotes: 0