Press "Enter" to skip to content

Writing a DNS Look-up Tool

As part of my day-job, I perform a large number of DNS look-ups. Sometimes these are done to diagnose complex DNS issues and tests need to be run from various servers by various methods, but most of the time I’m simply looking for the current information to check everything is configured correctly.

If you’re not sure what DNS is, the Wikipedia article is a good place to start, but essentially every domain name has a number of records behind it (such as A records for web traffic, and MX records for email) which tell it where to direct certain types of traffic. When you type a domain into your web browser, it is DNS which translates this to the IP address of the server, allowing you to reach a website without having to know this information yourself.

I’ve used a wide variety of DNS look-up tools online, all of which have their pros and cons, but for a long time I’d been looking to build one of my own, which I could customize to my specific needs, so I set out to learn how they are built. My PHP skills are barely adequate, so this could also give me some much needed coding practice.

I started by creating a PHP script and adding a very simple HTML form, to provide somewhere for users to enter the domain they’re interested in and POST this ‘domain’ variable back to the script:

<form action=”dns-lookup.php” method=”post”>
<table style=”width:500px”>
<td width=”30%” align=”right”>Domain Name:</td>
<td width=”40%”><input type=”text” name=”domain” value=”” /></td>
<td width=”30%”><input type=”submit” value=”Submit” />

From this point, I wasn’t sure quite how PHP could be used to grab the relevant information on IP addresses and hostnames. Obviously this information is available to the server, so I’d imagined some kind of process where the system runs a dig or similar, then pipes the output back to the script. However this would be incredibly clunky. I soon discovered the dns-get-record function, which PHP can use to perform lookups on various DNS types.

To take the most basic example, if I want to return the IP addresses for the A records on a domain name, I can use the DNS_A parameter. In order to perform the look-up on the domain we entered into our form, we need to use $_POST[‘domain’] to grab the contents of the form we submitted and feed these into the dns-get-record command. As there are commonly multiple A records, we also start a foreach loop, to print the IP address for each one:

$get_A_records = dns_get_record($_POST[‘domain’], DNS_A);

foreach ($get_A_records as $Arecord) {
echo “” . $Arecord[‘ip’] . “\n<br>”;

This command will spit out a simple list of IP addresses.

This might be adequate, but we also commonly want to know what the name of this server is, which we can get using the gethostbyaddr command. We set a variable of $hostname and feed each IP address into the foreach loop:

$get_A_records = dns_get_record($_POST[‘domain’], DNS_A);

foreach ($get_A_records as $Arecord) {
$hostname = gethostbyaddr($Arecord[‘ip’]);
echo “” . $Arecord[‘ip’] . ” (” . $hostname . “)” . “\n<br>”;

This will return the hostname of the server alongside the IP address: ( ( (

We can then clean our code up a little more, by sanitizing the input, and returning a value to inform the user when there are no results. We can use the trim command to strip initial and trailing blank characters (which happens often when you’re copying and pasting domain names in) and we can create an if statement which will return a message informing us if no records of that type exist:

$get_A_records = dns_get_record(trim($_POST[‘domain’]), DNS_A);

if (empty($get_A_records)) {
echo “<font color=grey>[None Found!]</font>”;
foreach ($get_A_records as $Arecord) {
$hostname = gethostbyaddr($Arecord[‘ip’]);
echo “” . $Arecord[‘ip’] . ” (” . $hostname . “)” . “\n<br>”;

We can then use the other parameters of the dns_get_record command to get other information, such as CNAME records, TXT records and MX records. I have included below an example of how the code can be adapted to obtain all of the mail-related MX records for a domain. You should consult the documentation on the function to see which specific arrays are available for each record type.

For example, MX records have a target address and – unlike A records – a priority, which tells mail servers which address to attempt delivery to in the first instance. Upon failure, delivery will be attempted at the next address:

$get_MX_records = dns_get_record(trim($_POST[‘domain’]), DNS_MX);
if (empty($get_MX_records)) {
echo “<font color=grey>[None Found!]</font>”;
foreach ($get_MX_records as $MXrecord) {
echo “” . $MXrecord[‘target’] . ” (” . $MXrecord[‘pri’] . “)\n<br>”;

Which returns the mail servers, and the priority in brackets:


The other functionality I wanted to include in my tool was a whois look-up. This is something which is slightly trickier to do in PHP, as whois is essentially its own protocol running on port 43. From all I’ve read, there doesn’t seem to be any ‘correct’ way to perform a whois look-up in PHP, as every method relies on connecting to the whois server for the top-level-domain (TLD) and manipulating the returned data.

The two primary ways of accessing whois data seem to be using the CURL or FOPEN functions in PHP to open a connection to a remote whois server. I have found that FOPEN seems to work far better for accessing all of the different servers, as I found CURL struggled with the .org and .info servers.

Given that any script requires a manual list of whois servers for each TLD, it is probably better to use an existing script and adapt this for your needs. The only issue is that it is very rare to find a script which has no bugs present. I experimented with a few scripts, some of which I spent hours trying to fix. I ended up using a class from, which uses FOPEN. I made several modifications to this, including forcing it to return to the user the specific whois server it was using, so they are aware of where the information is coming from. My own script then simply passes the $domain variable to the whois script.

You’ll also need to ensure you update your script as new TLDs are released. For example, I had to add the whois server for .io domains, as this was missing from the script on 99webtools.

Two other things I learnt whilst playing around with whois in PHP is to use the <pre> tag in the HTML to return the data in the exact format it is sent by the whois server (as the servers return pre-formatted text) and I found a neat CSS fix to prevent the text leaping outside of the tables I’d built. The other thing I found is that the whois server for .com domains – unlike any other – performs a wildcard search by default. So when you search for a popular domain such as, many irrelevant results are returned, as they happen to contain the ‘’ string inside them somewhere. Most of the scripts I found were vulnerable to this bug, but it is easy enough to fix using an if statement to prefix the word domain to the search string, where it is being sent to a .com whois server (eg: “domain”).

Here’s my completed DNS tool searching for this domain:


I hope this has proved a useful introduction to performing your own DNS and Whois look-ups with PHP. The code I’ve written above can be used as a useful start on building your own DNS tool which you can then customize to your hearts content. If you have any questions, please comment below.

You can test out my own DNS tool here:


  1. Gaurav Gokhale Gaurav Gokhale

    Hello Jonathan. I am new to this field of analyzing dns lookup. If you could provide me with the source code i would get a better insight on this. Could you please provide me with the complete source code it would help me a great deal.
    Thank you

  2. Sven Sven

    Hey Jonathan , you can send me the code of your DNS look-up tool?


  3. Max Max

    I’m interested too, may we have the code? Thanks!

  4. Al Al

    Me too if that’s OK…?

  5. Dan Dan

    Please send me the source too if that is alright, I am really interested to see how it all links together!

  6. Robby Robby

    Nice script do you have the source code?

  7. Hello People,

    Read the tutorial. Last weekend i take the time to read. In 3 hours i build a dns lookup. Thansk to this tutorial.

  8. you can send me the code of your DNS look-up tool?


  9. Chris Chris

    Hi Jonathan,
    Great post , I found it when searching for a method to create a web page that does a nameserver lookup. Are you able to share that part of your code ?

  10. Yoann Yoann

    Hi Jonathan,
    Thank you a lot for your article. I manage to to make some check.
    But i didn’t success to properly display the result (css etc…)
    Please can you send me your code that i can fix that.
    It would be greatly appreciate.

  11. Wes Brown Wes Brown


    Your WHOIS PHP script is brilliant, is there a chance you could send me the source code?



  12. shyam shyam

    hiiii sir,
    This is good script but can you send me source code i dont know how to use

  13. Joe Joe

    Love your script.
    Could you possibly send me the PHP script


  14. may i have the script please

  15. wtf the owner of “” copy everything?

    srsly, free script sir Jonathan?

  16. @ScanneD, read the tut and you get the same… Only i change some little things. I get weekly requests to send the source code. Please read this page and you can build it self!

  17. Wessel de Haan Wessel de Haan

    Hi Jonathan,

    Can i get a copy of the source ? or a better sample of the code you used ?
    I Will pay for it.

    Br, Wessel de Haan

  18. Shane Shane

    Would you mind if i had a copy of the code for my own site?


  19. Dug Yum Dug Yum

    Hello. I recently came across this wonderful tool and I really enjoy it. A source code download would be appreciated. Thanks!

  20. Hey Jonathan , can you send me the code of your DNS look-up tool?


  21. Hey Jonathan, Any chance you could provide the source code? It has been requested a number of times and would be greatly appreciated.

  22. Andrew Andrew


    Is there any source code foe this the one that robby listed doesnt seem to work.

  23. The script is awesome. I read it and i made it success. Thanks a lot.
    If possible can you share some more script regrading DNS server ?

  24. This looks perfect! Although there are several free tools available on the internet, having one for yourself is much better. Thanks a lot!

  25. Infotecnica Infotecnica

    Hello I downloaded the script
    However, the mx record does not report mx ip as it could be added to the code.
    I would also like to know if it is possible to add PTR entry for verification added to the code but I am not having a return of the reverse dns this is important to know the status of the reverse dns if they are ok.
    One more doubt to add the TLL to return the name servers
    If anyone can help I will be grateful.
    TLL I managed to add to the code but the return of the TLL is not matching the real configuration in the dns zone.
    Actual return example [TTL = 86400]
    My code returns [TTL = 21599]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.