4

Is there a way to retrieve current system fully qualified domain name using php-cli?

What I'm looking for is php function to retrieve the same thing that hostname -f is doing:

$ hostname -f  #returns needed: system01.dev.lan

I tried following:

php_uname('n') // returns: system01
gethostname() // returns: system01
gethostbyname(gethostname()) // returns ip address
gethostbyname(gethostname() . '.') // returns system01

$_SERVER dose not exist (because only cli env)

Again, I know I can do this

php -r "var_dump(shell_exec('hostname -f'));"

But for this trivial task, I think php should have built in functionality to retreive fqdn.

wormhit
  • 3,687
  • 37
  • 46

3 Answers3

10
gethostbyaddr(gethostbyname(gethostname()));

This returns the FQDN for me while everything else just returns localhost or hostname.

user740521
  • 1,175
  • 4
  • 12
  • 25
3

You can use the following …

echo gethostbyaddr("127.0.0.1");

This may give a you FQDN, or localhost (see below for the cause of this).

but:

even if this works, it is not guaranteed to, and is not at all portable.

Why is that?

What you want is basically a reverse DNS lookup for the IP address 127.0.0.1. It doesn't matter if you use hostname -f (if available in your hostname implementation) or gethostbyaddr("127.0.0.1"), the system must always occupy the resolver to find a FQDN for an IP address.

Now if you have a FQDN entry for your IP address in your /etc/hosts, then the resolver is able to find the FQDN locally, otherwise it must make a reverse DNS lookup.

This is by design, nothing you can do about it. Fully qualified domain names are the business of DNS, local hosts were never supposed to know about them. Even the /etc/hosts entry is more of a hack. (Though a common one, many mail servers need it, too.)

That's why the solution is not portable: If your application is deployed on a server where no FQDN for 127.0.0.1 is in the /etc/hosts file, then it will just return the simple host name.

And, obviously, a DNS server can't give you a domain name for a 127.0.0.1 lookup.

There's a little difference between PHP's gethostbyaddr('127.0.0.1') and hostname -f: The PHP implementation takes the first name assigned to 127.0.0.1, no matter if FQDN or not; hostname -f tries to find a FQDN in the names and uses the first one it finds.

The shell_exec approach appears valid, but if you'd need to avoid shell commands, you could try reading and parsing /etc/hosts directly in PHP; as the syntax is quite simple.

lxg
  • 12,375
  • 12
  • 51
  • 73
  • I don’t have `-f` flag on `hostname`, so I guess that would not be present on every machine either. – Smar Sep 02 '14 at 13:56
  • The long version is `--fqdn`, but maybe that's only present in the Debian implementation. Doesn't matter anyway, because it usually you would use something like `dig` for DNS queries on the CLI. – lxg Sep 02 '14 at 14:02
  • Yup, that’s not here either. But dig is not really installed by default either, so based on that, I’d really go with your answer over those two. – Smar Sep 02 '14 at 16:04
  • Thank you for explicit answer! gethostbyaddr("127.0.0.1") gave me localhost. So I'm sticking with shell_exec() that gives me needed result. I will always have there debian, so it is ok. At least now I understand why it is like it is. :) – wormhit Sep 03 '14 at 10:44
  • Yup, I wasn't aware of this difference between `gethostbyaddr('127.0.0.1')` and `hostname -f`. I updated the post accordingly, see added two paragraphs at the end. – lxg Sep 03 '14 at 11:39
1

For debian default config the fqdn can be retrieved with:

gethostbyaddr('127.0.1.1');
taur
  • 169
  • 1
  • 7