PHP.Working with DNS servers
In that article we’ll talk about PHP functions for working with DNS servers. The most frequent task is getting host name by its IP-address, other tasks are less frequent.
- gethostbyname function
- gethostbynamel function
- gethostbyaddr function
- checkdnsrr function
string checkdnsrr(string hostname [, string type])
DNS server saves a lot of useful information about host. That’s why so called recourse notations are used and they have the following types:
- A (Notation includes host IP-address)
- CNAME (Notation includes host pseudonym)
- NS (Notation includes DNS server’s name)
- MX (Notation includes name of the domain mail relay host)
Syntax:
string gethostbyname(string hostname)
Example of gethostbyname function use:
<?
$hostname = "localhost";
$ip_address = gethostbyname($hostname);
echo ("IP-address? $hostname: $ip_address");
?>
string gethostbynamel(string hostname)
Many computers have several IP-addresses. Such situation is typical for different servers. If you want to get the full list of IP-addresses of the given computer you have to use gethostbynamel function that works similar to gethostbyname function. Another situation of using that function is when one DNS name conforms to several computers.
Return list of the IP-addresses gethostbynamel function puts into the array:
<?
$hostname = "localhost";
$ip_addresses = gethostbyname($hostname);
echo("The IP adresses of "$hostName" are: <br>\n");
foreach($ip_adresses as $index => $val)
{
echo("$val");
}
?>
That function considers IP-address as an argument and returns its corresponding host name:
<?
$ip_address = "127.0.0.1";
$hostname = gethostbyaddr ($ip_address);
echo ("Host name with IP-address $ip_address: $hostname");
?>



