[英]IPv4 address with PHP
I want to get the IPv4 address, but not the localhost address (127.0.0.1).我想获取 IPv4 地址,而不是本地主机地址 (127.0.0.1)。 I just get ::1.我只得到 ::1。
i tried it with $ip = getenv ("REMOTE_ADDR");
我用$ip = getenv ("REMOTE_ADDR");
试过了$ip = getenv ("REMOTE_ADDR");
and和
$ip = getenv ('SERVER_ADDR');
$ip = $_SERVER['REMOTE_ADDR'];
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;
}
您可以使用gethostbyname()
获得 IPv4 地址,例如使用gethostbyname($_SERVER['HTTP_HOST'])
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.