php MAMP 应该返回 ::1 作为本地主机上的 IP 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3699454/
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
Should a MAMP return ::1 as IP on localhost?
提问by Mild Fuzz
I need to retrieve a user's IP. My localhost is returning ::1
as the IP. I am curious, if this is an expected behaviour? I'm running MAMP on Snow Leopard.
我需要检索用户的 IP。我的本地主机::1
作为 IP返回。我很好奇,这是否是预期的行为?我在雪豹上运行 MAMP。
回答by Bill Ortell
Same question, and found a valid solution, tested, works well. I wanted to have the ip addy - of 127.0.0.1 as well instead of having to accept the the ::1 and debate the whole IPv4/6 issues. So, i trolled for a few moments and fell onto a 2008 comment made by @Brad - here: http://board.issociate.de/thread/489575/SERVERquotREMOTEADDRquot-returning-1.html
同样的问题,找到了一个有效的解决方案,经过测试,效果很好。我也想拥有 127.0.0.1 的 ip addy,而不必接受 ::1 并辩论整个 IPv4/6 问题。所以,我拖了一会儿,发现了@Brad 在 2008 年发表的评论 - 在这里:http://board.issociate.de/thread/489575/SERVERquotREMOTEADDRquot-returning-1.html
Summarizing - (on Mac OS - Mountain Lion in particular)
总结 -(在 Mac OS 上 - 特别是 Mountain Lion)
sudo vi /etc/apache2/httpd.conf
sudo vi /etc/apache2/httpd.conf
Find where your apache is 'listen'-ing to the ips/ports, etc... Typically this will be a line looking like this
找到你的 apache 在哪里“监听”ips/端口等......通常这将是一条看起来像这样的线
Listen 80
Listen 80
Make it look like this:
让它看起来像这样:
Listen 127.0.0.1:80
Listen 127.0.0.1:80
Resave it. Restart Apache. Voila!
重新保存。重新启动阿帕奇。瞧!
Now $_SERVER[REMOTE_ADDR]
will look like this 127.0.0.1
.
现在$_SERVER[REMOTE_ADDR]
看起来像这样127.0.0.1
。
Hope it helps someone.
希望它可以帮助某人。
回答by Tomasz Kowalczyk
::1
is the IPv6 equivalent of IPv4 127.0.0.1
address.
::1
是 IPv4127.0.0.1
地址的 IPv6 等价物。
You can convert IPv6 address to IPv4 address using function inet_ntop()
as Adam, said.
您可以使用inet_ntop()
Adam 所说的功能将 IPv6 地址转换为 IPv4 地址。
回答by Isaias Soares
Modifying the /etc/apache2/httpd.conf
or even /private/etc/hosts
isn't the solution. It's returning ::1 because it's the IPv6 equivalent to the old (but gold) IPv4's 127.0.0.1
. As it's defined in the /privates/etc/hosts
, whenever you access http://localhost
it assumes IPv6 ::1. So it's the expected behavior.
修改/etc/apache2/httpd.conf
甚至/private/etc/hosts
不是解决方案。它返回 ::1 因为它是 IPv6 等效于旧的(但黄金)的 IPv4's 127.0.0.1
。正如它在 中定义的那样/privates/etc/hosts
,无论何时您访问http://localhost
它都假定 IPv6 ::1。所以这是预期的行为。
A workaround would be to access http://127.0.0.1
directly. It'll work just fine. But remember that IPv6 availability is increasing. You could make sure your server (after deployment) will only respond to IPv4 but in the mid term, if your site/web app does not handle both IPv4 and IPv6 properly, there's a great chance something will go wrong within 1/2 years. Or sooner, we never know.
一种解决方法是http://127.0.0.1
直接访问。它会工作得很好。但请记住,IPv6 的可用性正在增加。您可以确保您的服务器(部署后)仅响应 IPv4,但在中期,如果您的站点/Web 应用程序不能正确处理 IPv4 和 IPv6,则很有可能在 1/2 年内出现问题。或者更早,我们永远不会知道。
回答by K3rnel31
for who has not reslove this issue it sounds that the hosts
file is missing or the line
对于谁还没有解决这个问题,听起来hosts
文件丢失或行
127.0.0.1 localhost
in etc\hosts
LINUX
在etc\hosts
LINUX
in /private/etc/hosts
MAC devices
在 /private/etc/hosts
MAC 设备中
in \%WINDIR%\system32\drivers\etc\hosts
WINDOWS xp/vista/7/8/
在\%WINDIR%\system32\drivers\etc\hosts
WINDOWS xp/vista/7/8/
whene the file hosts
is removed or missed
or the line too the IPV6
will be set by default
...
当文件hosts
被删除或missed
行也IPV6
将被设置default
...
回答by Zubair1
You have to disable IPv6 support in your OS for apache to stop showing these IPv6 format addresses, when you want the IPv4 format. I disabled IPv6 support and its working as expected now.
当您需要 IPv4 格式时,您必须在操作系统中为 apache 禁用 IPv6 支持才能停止显示这些 IPv6 格式地址。我禁用了 IPv6 支持,它现在按预期工作。
I recently ran into this issue, though i don't ever remember enabling ipv6 support on my Windows 7 system, could be done by Microsoft in one of their updates (maybe).
我最近遇到了这个问题,虽然我不记得在我的 Windows 7 系统上启用 ipv6 支持,但可以由 Microsoft 在他们的更新之一中完成(也许)。