Home arrow PHP arrow Page 2 - PHP Networking


PHP has a great many tools for interacting with a network and also with the Internet. In this article we will look at some of those tools and functions to see how we can use them to make our scripts more useful in a network environment. This article is the first of two parts.

  1. PHP Networking
  2. DNS and PHP
  3. DNS and PHP continued
  4. Getting information about a domain name
By: David Web
Rating: starstarstarstarstar / 4
September 08, 2008

print this article



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):


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:


$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.


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:


$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/.


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:


$ip = "";

$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, which is always the localhost machine, that is, the machine on which the script was run.

>>> More PHP Articles          >>> More By David Web

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: