PHP 中机器的 IP 地址给出了 ::1 但为什么呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10517371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IP Address of the machine in PHP gives ::1 but why?
提问by John
I am trying to fetch the ip address of my machine through php. For that I am writing the code like:
我正在尝试通过 php 获取我机器的 ip 地址。为此,我正在编写如下代码:
<?php echo "<br />".$_SERVER['REMOTE_ADDR'];?>
But this piece of code is not working. It is returning "::1".
Please help me how to get the actual IP Address.
但是这段代码不起作用。它正在返回“ ::1”。请帮助我如何获取实际的 IP 地址。
回答by Quentin
::1is the actual IP. It is an ipv6 loopback address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1.
::1是实际IP。它是一个 ipv6 环回地址(即 localhost)。如果您使用的是 ipv4,它将是127.0.0.1.
If you want to get a different IP address, then you'll need to connect to the server through a different network interface.
如果您想获得不同的 IP 地址,则需要通过不同的网络接口连接到服务器。
回答by Yigit Tanriverdi
If you are trying to run localhost, this answer will fix your problem. Just few changes on
如果您尝试运行 localhost,此答案将解决您的问题。只是一些变化
apache2/httpd.conf
search all "listen" words ex:
搜索所有“听”字例如:
Listen 80
Make like this.
弄成这样。
Listen 127.0.0.1:80
than restart your apache
比重启你的apache
$_SERVER[REMOTE_ADDR]
will show Listen 127.0.0.1
将会呈现 Listen 127.0.0.1
you can see answer in this detailed answer link
您可以在此详细答案链接中看到答案
回答by Guillaume Flandre
If you mean getting the user's IP address, you can do something like :
如果您的意思是获取用户的 IP 地址,您可以执行以下操作:
<?php
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
$ip=$_SERVER['REMOTE_ADDR'];
}
?>
<?php echo "<br />".$ip;?>
It will get the user's actual IP address, regardless of proxies etc.
它将获取用户的实际 IP 地址,而不管代理等。
回答by Tushar
$_SERVER['REMOTE_ADDR'] is the IP address of the client.
$_SERVER['REMOTE_ADDR'] 是客户端的 IP 地址。
$_SERVER['SERVER_ADDR'] is the IP address of the server.
$_SERVER['SERVER_ADDR'] 是服务器的 IP 地址。
Reference: http://php.net/manual/en/reserved.variables.server.php
回答by dotancohen
Look at the output of phpinfo(). If the address is not on that page, then the address is not available directly through PHP.
看看输出phpinfo()。如果该地址不在该页面上,则无法直接通过 PHP 获得该地址。
回答by Hyman Larson
Simple answer: You are using it on local server. Try running
简单回答:您在本地服务器上使用它。尝试跑步
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
echo 'User Real IP - '.getUserIpAddr();
in real server. Or you can also user online php executor.
在真实服务器中。或者您也可以使用在线 php 执行器。

