PHP provides a powerful set of functions for DNS name resolution. Though the functions that are provided by PHP are limited to DNS client functionality and no server related functions are available, this is adequate since most applications will only require a name resolution service. Below is a list of some of the functions that are available in PHP (from the PHP manual): gethostbyname() string gethostbyname(string hostname) This function takes a single argument, which is the hostname of a machine, and returns a string corresponding to the IP address: <?php $host = "localhost"; $ip = gethostbyname($host); echo "The IP address of $host is $ip"; ?> This script displays the IP address of http://localhost as a dot-separated string of numbers. We need to obtain the IP address of a host before we can connect to it using the sockets API, as we shall soon see. gethostbynamel() array gethostbynamel(string hostname) Machines running operating systems that support virtual IP addresses (that is, one network card can have multiple IP addresses) can have more than one IP address associated with them. In this case, gethostbynamel() works like gethostbyname() but returns the complete list of IP addresses associated with that hostname as an array: <?php $host= "localhost"; $ip= gethostbynamel($host); echo("The IP addresses of $hostare:<br>n"); for ($i = 0; $i < count($ip); $i++) { echo("$ip [i] <br>n"); } ?> Assuming that web.local.com is a machine with multiple IP addresses, this script prints a list of all those addresses. The other scenario where one DNS name maps onto multiple IP addresses is when certain DNS server implementations (such as BIND) support a feature known as DNS round robin. This is a rudimentary load-balancing mechanism where one DNS name maps to multiple machines (that is, IP addresses of multiple machines). The DNS queries are resolved by the server to each of these IP addresses using a round robin scheme, thereby distributing the request load between multiple machines. More information on BIND is available at http://www.isc.org/products/BIND/. gethostbyaddr() string gethostbyaddr(string ip_address) This function does the reverse of gethostbyname() in that, given the IP address as an argument, it returns the host name corresponding to it: <?php $ip = "127.0.0.1"; $host= gethostbyaddr($ip); echo("The host name corresponding to the IP address $ip is $host"); ?> This script displays the hostname corresponding to the IP address 127.0.0.1, which is always the localhost machine, that is, the machine on which the script was run.
blog comments powered by Disqus |
|
|
|
|
|
|
|