Search code examples
phppinggethostbyname

Why does getHostByName() return a different result than ping?


When I ping my domain name from my computer, I get a different IP than if I run a script on my host that does

echo getHostByName(getHostName());

Why is this?


Solution

  • The server's hostname can be different from DNS records pointing to this server. This is easy to see: Multiple domains can point to a server, but the server itself can only have one name.

    Most likely the hostname that is configured on the server (and thus returned by gethostname) is different than the one you are pinging.

    Another possibility is that the internal DNS resolver returns different IP addresses for internal hosts than the external DNS server would. This is known as split-horizon DNS.