How to query DNS records using PHP

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to query DNS records using PHP

DNS records are meant to be stored on a DNS server, using these servers you can easily get specific DNS records for your own application, apply specific limits or the like. In my license degree I remembered making a similar cross-platform app that would debug a domain DNS records and let’s you know what is wrong with it. I believe that was the start of me fully understanding how the DNS (Domain name server) works. In this article I’m going to show you some ways to get or query DNS records using PHP. Process is really simple and can be used in a lot of  different applications.

PHP Functions that can be used

There are a few functions for different basic checks that you can use for these type of queries, each of them for different DNS checks. Bellow you have a list that we will go step by step with examples:

  • checkdnsrr() – Check if DNS records exists corresponding to a given Internet host name or IP address
  • dns_get_record() – Fetch DNS Resource Records associated with a hostname
  • getmxrr() – Get MX records corresponding to a given Internet host name
  • gethostbyaddr() – Get the Internet host name corresponding to a given IP address
  • gethostbyname() – Get the IPv4 address corresponding to a given Internet host name
  • gethostbynamel() – Get a list of IPv4 addresses corresponding to a given Internet host name

checkdnsrr() function

This one holds the possibility of checking almost every DNS records that you can think of if they exist on the specific domain name, the parameters for this functions are:

  • host – This can be either the IP address ( or the hostname (
  • type – This is the type of check you wish to do, which can be one of:

To check all records you can use the ANY type parameters or if you need specific records you can use for eg. MX. If a record is found, it will return true, else it will return false. There is another function called dns_check_record() which does the exact same thing.

Code example:

 ↠ Continue with dns_get_record()

Pages: 1 2 3

Request an article ←