How to query DNS records using PHP

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

getmxrr() functions

This function will get us the MX records for the domain name we are checking, the parameters that can be used with this are bellow:

  • hostname – this should be the DNS hostname we wish to check the MX records for, eg. google.com
  • mxhosts – in this variable, if used, we will have an array of all the mx records found
  • weight – if we use the weight array, it will be filled with the weight information gathered.

This is also simple, see below example code:

Output for this would be:

Simple and easy, right?

gethostbyaddr() and gethostbyname() functions

I’m going to show you both functions as these are really similar, both complementing each other. The first function, gethostbyaddr() will return the reverse dns of the IP address we are checking for, while the second function, gethostbyname() will provide you the IP address for the hostname you provided.

Example code:

Now the IP addresses I used are different, don’t forget now that google has lots of IPs added to its domain. This is where our last function comes in.

gethostbynamel() function

This function will provide us with a list of all the IP addresses found on the hostname we are checking, for instance, let’s check for google.com as it has more IPs.

Example code:

Output would be:

As you can see, it will list all the IP addresses it will find on the given hostname in a nice little array.

All these function can be used to create a really nice application, there are a few ones I know of that can be made using php, this one:

http://network-tools.com/nslook/

using using ASP code, similar to how I showed you in this article, you can use these functions to determine a specific hostname DNS record and make checks on it and debug them.

That’s it for today’s article, see you again tomorrow in a new one!

↠ Go back to first page

Pages: 1 2 3

Request an article ←