PHP gethostbyname() and DNS

by damonp on August 23, 2007

in Development,PHP

For PHP’s gethostbyname() to work properly, the server’s DNS must be properly configured with available nameservers in /etc/resolv.conf (on Linux boxes). Without a work domain name resolution kit, gethostbyname() returns the hostname supplied to the function.

Make sure /etc/resolv.conf contains several nameservers to query. The format is

nameserver xxx.xxx.xxx.xxx
nameserver yyy.yyy.yyy.yyy

Where xxx.xxx.xxx.xxx and yyy.yyy.yyy.yyy are the IP addresses of nameservers the host has permission to use.

OpenDNS is a free DNS service that allows public access to their nameservers at:

208.67.222.222
208.67.220.220

To use in /etc/resolv.conf use:

nameserver 208.67.222.222
nameserver 208.67.222.220

Using OpenDNS’s nameservers could even boost your server’s performance. Their goal is to provide some of the faster domain name resolvers on the internet for free.

Popularity: 2%

Most Popular Posts

Damon Parker is a freelance sysadmin and web developer in Texas. He specializes in server setup, server security and high performance server configurations. Need help setting up a web server or getting a server back online after a crash or hack? Email Damon

{ 6 comments… read them below or add one }

Pavel Titov August 23, 2007 at 6:51 pm

To boost server’s performance it’s better to have a local (preferably within the same network segment) caching nameserver. BTW it can forward requests to the OpenDNS, although I prefer using co-location facility’s DNS.

Reply

damonp August 27, 2007 at 12:16 pm

Local nameservers are a better setup if the datacenter ISP maintains a high QOS on its DNS servers and doesn’t overload them with too many hosts for each nameserver. Many ISPs don’t which is why I’ve seen better response in a lot of cases using a fast service like OpenDNS.

Reply

Pavel Titov August 27, 2007 at 12:26 pm

In this case it’s still worth to set up own caching NS and forward requests to OpenDNS.

Reply

damonp August 27, 2007 at 1:41 pm

For optimum performance… yes. However, setting up a caching nameserver takes time (thus $$) and some know how or you can totally hose your server. Using a third party DNS takes two seconds and almost anyone can follow the few steps it takes to configure.

If everyone could setup a nameserver properly on their own, there are a lot of us that would be out of a job. :-(

Reply

pk May 1, 2011 at 11:19 pm

Hi. My /etc/resolv.conf currently shows the IP addresses of my own server. (I am on the usual Cpanel/WHM thing). Can I simply add the OpenDNS stuff you mention above?

Thanks!

Reply

damonp May 2, 2011 at 7:35 am

Yes you can add the OpenDNS records below the ones already in the file. The resolver will try them in the order listed in the file until it receives a valid response.

Reply

Leave a Comment

Previous post:

Next post: