PHP Internet Services
Performing DNS Lookups
Problem
You want to look up a domain name or an IP address.
Solution
Use gethostbyname() and gethostbyaddr():
$ip = gethostbyname('www.example.com'); // 93.184.216.119
$host = gethostbyaddr('93.184.216.119'); // www.example.com
Discussion
You can generally trust the address returned by gethostbyname(), but you can’t trust the name returned by gethostbyaddr(). A DNS server with authority for a particular IP address can return any hostname at all. Usually, administrators set up DNS servers to reply with a correct hostname, but a malicious user may configure her DNS server to reply with incorrect hostnames. One way to combat this trickery is to call gethost byname() on the hostname returned from gethostbyaddr() and make sure the name resolves to the original IP address.
If either function can’t successfully look up the IP address or the domain name, it doesn’t return false, but instead returns the argument passed to it. To check for failure, do this:
$host = 'this is not a good host name!';
if ($host == ($ip = gethostbyname($host))) {
// failure
}
This assigns the return value of gethostbyname() to $ip and also checks that $ip is not equal to the original $host.
Sometimes a single hostname can map to multiple IP addresses. To find all hosts, use gethostbynamel():
$hosts = gethostbynamel('www.yahoo.com');
print_r($hosts);
This prints something like the following (the specific IP addresses may vary based on your location):
Array
(
[0] => 98.139.183.24
[1] => 98.139.180.149
)
In contrast to gethostbyname() and gethostbyaddr(), gethostbynamel() returns an array, not a string.
You can also do more complicated DNS-related tasks. For instance, you can get the MX records using getmxrr():
getmxrr('yahoo.com', $hosts, $weight);
for ($i = 0; $i < count($hosts); $i++) {
echo "$weight[$i] $hosts[$i]\n";
}
This prints:
Whereas gethostbyname() retrieves IPv4 A records, and getmxrr() retrieves MX records, the dns_get_record() function retrieves whichever type of DNS record you specify. This is useful, for example, to retrieve IPv6 AAAA records, as follows:
$addrs = dns_get_record('www.yahoo.com', DNS_AAAA);
print_r($addrs);
This prints something like the following (again, the specifics will vary based on your location):
Array
(
[0] => Array
(
[host] => ds-any-fp3-real.wa1.b.yahoo.com
[type] => AAAA
[ipv6] => 2001:4998:f00b:1fe::3001
[class] => IN
[ttl] => 34
)
[1] => Array
(
[host] => ds-any-fp3-real.wa1.b.yahoo.com
[type] => AAAA
[ipv6] => 2001:4998:f00d:1fe::3001
[class] => IN
[ttl] => 34
)
[2] => Array
(
[host] => ds-any-fp3-real.wa1.b.yahoo.com
[type] => AAAA
[ipv6] => 2001:4998:f00d:1fe::3000
[class] => IN
[ttl] => 34
)
[3] => Array
(
[host] => ds-any-fp3-real.wa1.b.yahoo.com
[type] => AAAA
[ipv6] => 2001:4998:f00b:1fe::3000
[class] => IN
[ttl] => 34
)
)
Each element of the returned array is a subarray containing information about the record type, hostname, IPv6 address, record class, and the TTL—how long the record is cacheable for.
Read the manual page for dns_get_record() to learn how to indicate which type of DNS record you are interested in.
To perform zone transfers, dynamic DNS updates, and more, see PEAR’s Net_DNS2 package.
No comments:
Post a Comment