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.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Host and IP resolution >>
Previous chapter: Sockets can be powerful
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.