Hudzilla.org - the homepage of Paul Hudson
Contents > Networks Wish List | Report Bug | About Me ]

15.2     Domain resolution functions: dns_check_record(), dns_get_mx(), and dns_get_record()

This is NOT the latest copy of this book; click here for the latest version.

int dns_check_record ( string host [, string type])

int dns_get_mx ( string hostname, array mxhosts [, array &weight])

int dns_get_record ( string hostname [, int type [, array &authns, array &addtl]])

There are three helpful Domain Name System (DNS) functions that allow you to check and retrieve specific information about other servers on the Internet or the local network. These are dns_check_record(), dns_get_mx(), dns_get_record(), and, sadly, all three are not supported on Windows.

The first two from that list, dns_check_record() and dns_get_mx(), both work in roughly the same way - the former returns true if it finds a DNS record for the domain you specify in parameter one, and the latter returns true if it finds an MX record for the domain specified in parameter one, and places the full list of MX servers into a second parameter as an array.

If you did not know already, the Domain Name System (DNS), is what turns addresses such as www.slashdot.org into the computer-readable numbers that we call an IP address, such as 192.168.0.1. DNS servers store huge directories of these lookup tables, mapping domain names like slashdot.org to IP addresses, and it is these that are queried by your browser when you type your web address in.

DNS records come in several forms, the most popular of which are A, MX, NS, and CNAME - A records store normal WWW names, MX records store email information, NS returns the name servers that contain complete information on the domain, and CNAMEs return canonical names to where the domain actually is. You can, for example, have the A record for your site pointing to 192.168.0.1, and the MX record for the same site pointing to 192.168.0.2. The NS records generally point to the name servers for the hosting company who run the web site.

With that in mind, here's a little bit of example code to show off dns_check_record():

Author's Note: Be wary of cross-platform problems here -these functions don't work on Windows at the time of writing!

<?php
    
if (dns_check_record("snaps.php.net")) { print "Snaps.php.net exists\n"; }
    if (
dns_check_record("kde.org")) { print "KDE.org exists\n"; }
    if (
dns_check_record("fzzbcks.net")) { print "Fzzbcks.net exists\n"; }
?>

Moving on, dns_get_record() is a more complex version of dns_check_record() - it returns an array of the information it found, which itself contains an array of each record found. With the record arrays, the two key elements are target and type - type returns the record type that was found, whether that be A, NS, etc, and target, when sent with a CNAME record, shows what the domain actually maps to.

For example:

<?php
    var_dump
(dns_get_record("www.microsoft.com"));
?>

That simple script outputted the following information:

array(1) {
    [0]=>
    array(5) {
        ["host"]=>
        string(17) "www.microsoft.com"
        ["type"]=>
        string(5) "CNAME"
        ["target"]=>
        string(24) "www.microsoft.akadns.net"
        ["class"]=>
        string(2) "IN"
        ["ttl"]=>
        int(4568)
    }
}

What it says is that www.microsoft.com is just a pointer to www.microsoft.akadns.net - while you might appear to browse around www.microsoft.com, you are really being redirected to www.microsoft.akadns.net. Note that "akadns.net" is a signature that the site is actually being provided through a company called Akamai, which specialises in worldwide load balancing for the Internet. Microsoft does not always use Akamai, often serving its content from in-house machines, so your results may vary.





<< 15.1.5 Sockets can be powerful   15.3 Host and IP resolution: gethostbyaddr(), gethostbyname(), and gethostbynamel() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 07 Sep 2008

Wow. This site is really good. And to think I bought that crappy Luke Welling/ Laura Thomson book.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow