Team LiB
Previous Section Next Section

DNS/Reverse DNS Lookups

When you're working online, it is almost certain that at some point you'll need to work with DNS records. The topic of DNS, or the domain name service, is a complex one well beyond the scope of this book. However, in this section we discuss the tools available within PHP that allow you to retrieve a wealth of information from your DNS servers. Let's get started.

Retrieving the DNS Record by IP

Perhaps the most common of the functions introduced in this chapter is the gethostbyaddr() function. This function enables your PHP scripts to determine the domain name associated with a provided IP address. The syntax of this function is as follows:


$ipaddress is the IP address that you would like to resolve the domain name for. When executed, the gethostbyaddr() function returns the domain name associated with the IP or, if the domain could not be resolved, the passed IP address is returned. This function can be useful for determining more information about a visiting browser, as shown in Listing 21.1:

Listing 21.1. Determining the Hostname of a Remote IP

    $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);

    if($hostname === $_SERVER['REMOTE_ADDR']) {

        echo "The host name could not be resolved.<BR/>\n";

    } else {

        echo "The host name is: $hostname<BR/>\n";



Retrieving IP Addresses Based on Hostname

Just as PHP can look up domain names based on IP addresses, it can also look up IP addresses based on domain. This function is the gethostbyname() function with the following syntax:


$hostname is the hostname that you would like to determine the IP address for. On execution, the gethostbyname() function will return a string representing the IP address of the host or the same string passed as the $hostname parameter on failure (see Listing 21.2).

Listing 21.2. Reverse Lookup of IPs Based on Domain

    $ip_addr = gethostbyname("");

    if($ip_addr === "") {

        echo "Could not resolve the IP address for the host!<BR/>\n";

    } else {

        echo "The IP address for the host is: $ip_addr<BR/>\n";



Often, more than one IP address is associated with a given domain name. This is especially true for major websites such as Although the gethostbyname() function will retrieve one of these addresses, it will not provide the complete list of valid IP addresses associated with the domain. For this purpose, PHP provides the gethostbynamel() function:


Again, $hostname is the DNS name to resolve. Unlike gethostbyname(), the gethostbynamel() function, when executed, returns an array containing all the IP addresses associated with the domain name or a Boolean false on failure (see Listing 21.3):

Listing 21.3. Retrieving All IPs Associated with a Domain

    $hostname = "";

    $ip_addrs = gethostbynamel($hostname);

    if(!$ip_addrs) {

        echo "Could not resolve the domain name $hostname<BR/>\n";

    } else {

        echo "Here is a list of IPs associated with $hostname:<BR/><BR/>\n\n";

        foreach($ip_addrs as $ip) {

            echo "IP: $ip<BR/>\n";




Determining DNS Record Information


At the time of this writing, these functions are not available in Windows.

Although in general most PHP scripts are concerned only with resolving IP addresses to domain names (or the other way around), PHP offers many functions that assist you in digging into DNS records. The first of these functions, the dns_check_record(), allows you to check whether a particular type of DNS record exists for a given domain; use the following syntax:

dns_check_record($hostname [, $type]);

$hostname is the domain name to look up, and the optional parameter $type represents a string indicating the type of DNS record to check. A list of accepted values is found in Table 21.1.

Table 21.1. DNS Record Types and Their Meanings


Address code, used for storing an IP address associated with the domain


Mail exchange, the domain name used for sending and receiving mail


The authoritative name server


Start of Authority


Domain name pointer


Canonical name for a DNS alias


Address code used for IPv6 Addresses


Any of the above

If the $type parameter is not provided, by default the dns_check_record() function will default to MX. Listing 21.4 uses the dns_check_record() function to determine whether a domain name has a valid authoritative name server associated with it:

Listing 21.4. Using the dns_check_record() Function

    $hostname = "";

    if(dns_check_record($hostname, "NS")) {

        echo "An authoritative name server exists.\n";

    } else {
        echo "No name server was found for this domain\n";



To actually retrieve information about an existing DNS record (such as NS or A) the dns_get_record() function is used:

dns_get_record($hostname [, $type [, &$authns, &$addtl]]);

$hostname is the domain name to query, and the optional parameter $type is the type of information to retrieve. The final two parameters, $authns and $addtl, are pass-by-reference variables that will be populated with authoritative name servers and any additional records, respectively.

As the syntax of the dns_get_record() function indicates, you must pass variables for both $authns and $addtl if either is desired. Failing to pass two variables (for instance, attempting to pass just the $authns parameter) is incorrect.

This function will return a wealth of information regarding all the records associated with a particular domain name as an array of associative arrays. Although the specifics of the associative arrays will vary from record to record, each associative array will always contain at least the following key/value pairs (see Table 21.2):

Table 21.2. Universal Keys Found in Arrays Returned by dns_get_record()


The record in the DNS namespace that this data refers to.


In PHP, this will always be IN because the dns_get_record() function returns only Internet class records.


The record type, such as MX, CNAME, and so on.


The time to live for this record. Not equal to the original record TTL, but rather the amount of time left, based on when the authoritative name server was queried.


For a complete list of all possible associative keys available in a DNS record, consult the PHP manual for the dns_get_record() function.

By default, the dns_get_record() function returns all records associated with a given domain name. However, if specific information is desired (such as the mail exchange record MX), it may be retrieved by a constant prefixed with DNS_ to the $type parameter:


<TYPE> is MX, CNAME, or any other valid DNS record. To return all records, use DNS_ALL. However, the constant DNS_ANY is also available, which reflects the default behavior (see Listing 21.5).

Listing 21.5. Using the dns_get_record() Function

    $hostname = "";

    $records = dns_get_record($hostname, DNS_ALL);

    echo "The domain $hostname has the following DNS records: ";

    foreach($records as $record) {

        echo "{$record['type']} ";



Because the most common DNS record to retrieve is often the MX record (in order to determine the mail exchanger address), PHP provides a function specifically for that purpose. This function, dns_get_mx(), has the following syntax:

dns_get_mx($hostname [, $mxhosts [, $weight]]);

$hostname is the domain name to retrieve the MX record for. The $mxhosts optional parameter is a variable to populate with an array representing the MX hosts. The second optional parameter, $weight, is the weight assigned to each MX host. On execution, the dns_get_mx() function returns a Boolean value indicating whether the operation was executed successfully. Listing 21.6 uses this function to return the mail exchange servers associated with the domain name.


According to RFC 2821, if no mail exchange record exists, the default behavior is to use the domain address itself as the mail exchanger.

Listing 21.6. Using the dns_get_mx() Function

    $hostname = "";
    if(dns_get_mx($hostname, $mxhosts, $weights)) {

        foreach($mxhosts as $key => $host) {

            echo "Hostname: $host (Weight: {$weights[$key]}<BR/>\n";


    } else {

        echo "Could not find any MX records for $hostname\n";



    Team LiB
    Previous Section Next Section