If using gethostbyname against the name of the localhost is always giving you 127.0.0.1 but you want the DNS address instead, just put a dot at the end of the name. E.g.,
$foo = gethostbynamel("myhost.example.com");
print_r($foo);
...is giving you this:
Array
(
[0] => 127.0.0.1
)
Then put a dot at the end of the name:
$foo = gethostbynamel("myhost.example.com.");
print_r($foo);
...and now you get something like:
Array
(
[0] => 172.217.1.99
)