ping localhost 返回 ::1的导致不能打开http://localhost的原因及解决

虽然可以在浏览器中正常访问http://localhost 但用file,file_get_contents等函数打开http://localhost 异常。用127.0.0.1也可以打开,本地hosts指向也正常,尝试ping localhost结果返回的是::1,推测不是httpd和php的配置问题,是php不能识别::1所致

ping localhost
reply from ::1: times<1ms
reply from ::1: times<1ms
reply from ::1: times<1ms

应该是返回127.0.0.1,::1显然是错误的,不过为什么呢,hosts文件也没有错啊

127.0.0.1 localhost

这导致php的file_get_contents,getimagesize等函数不能打开“ http://localhost ”开头的,只可以打开“ http://127.0.0.1 ”开头的。

卸载ipv6:ipv6 uninstall

一切恢复正常。