Incidentally, if PHP doesn't offer the particular network function that you require, you can probably call it via one of PHP's system execution functions. You can use these execution functions to call various commands located on the server operating system itself. For example, Listing 1-6 illustrates the use of PHP's
exec() function to call the ping command, which sends a request for a server response. The
exec() function will then store all output into the second input parameter, allowing for subsequent storage, display, or analysis of this data.
Listing 1-6: Using exec() to display ping output
// ping some server five times and store output in array $output
exec("ping -c 5 www.oreillynet.com", $output);
// format each line of output
while (list(,$val) = each($output)) :
Executing Listing 1-6 will result in output similar to the following:
Pinging www.oreillynet.com with 32 bytes of data: Reply from www.oreillynet.com: bytes=32 time<10ms TTL=128 Reply from www.oreillynet.com: bytes=32 time=1ms TTL=128 Reply from www.oreillynet.com: bytes=32 time<10ms TTL=128 Reply from www.oreillynet.com: bytes=32 time<10ms TTL=128 Reply from www.oreillynet.com: bytes=32 time<10ms TTL=128 Ping statistics for www.oreillynet.com: Packets: Sent = 5, Received = 5, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 1ms, Average = 0ms
Note that I used the HTML "pre" tags to ensure that the output is displayed in some readable format. If these tags were omitted, the array contents would run together in a rather confusing jumble.
The French PHP resource site www.phpinfo.net offers for download a very slick user logging application written in PHP. This is something you should definitely check out.
Of course, you're not limited to calling just
ping with PHP's command functions; you can call any function available on the server.
Verifying domain availability
In today's continuing craze of gobbling up domain names, it becomes ever more necessary for ISPs to offer users the capability to easily verify whether or not a particular domain has already been taken. While on the surface this may seem like a complicated feature to implement, it's actually rather trivial. In this section, I'll demonstrate just how easy it is to incorporate this feature into your web site. First however, allow me to take a moment to explain the underlying mechanics of domain name storage and retrieval.
Although many think of the Internet as this chaotic mess of servers, all that we do today in terms of sending e-mail, surfing the Web, or using a search engine would be impossible if it weren't for organization starting on the highest levels. Operational centers known as NICs (Network Information Centers) are responsible for maintaining databases containing information regarding various networks and the domains and other data pertinent to these networks. Chances are you are familiar with NICs such as Network Solutions and Register.com. Of course, these NICs do not handle all domain extensions. You may need to modify Listing 1-7 (shown below) to be in accordance with the domain extensions that you'd like to search.
The databases that NICs administrate are typically known as WHOIS databases. Using PHP, it's easy to create a web interface that can interact with just such a database, querying it and subsequently displaying the data to the browser. Let's consider just such an interface, making use of the Internic WHOIS database to perform the query. First off, take a moment to try the demo, and then review the code in Listing 1-7.
Listing 1-7: A WHOIS interface (whois.php)
// Has the HTML form been viewed yet?
if (! $seenform) :
$form = <<<Form
<input type="hidden" name="seenform" value="y" />
Domain name: (i.e. oreillynet.com)<br />
<input type="text" name="domain" value="" size="55" maxsize="65" /><br />
<input type="submit" value="Look it up!" />
// The form has been viewed; Process information.
$fh = fsockopen("whois.internic.net", 43)
or die("Can't open WHOIS!");
if ($fh) :
while (! feof($fh)) :
echo fgets($fh, 1048);
I'll assume that you are familiar with many of the functions used in Listing 1-7, save for perhaps
fsockopen(). This function attempts to open a socket connection with some Internet domain. In the above example, I use
fsockopen() to open a connection on hostname "whois.internic.net" on port 43.
I'd also like to note that Listing 1-7 is for demonstration purposes only. Some NICs restrict usage of their WHOIS database. Therefore, if you're interested in integrating this type of functionality into your site, please check with the NIC before doing so.
This article touched upon networking as it relates to the PHP scripting language. Quite a few topics were touched upon, including PHP's
mail() function, bulk-e-mail software, e-mail validation, IP translation, PHP's system functions, and finally, verifying domain availablity via PHP. Hopefully I've provided you with some insight as to how to begin exploiting PHP's vast set of networking capabilities for your own purposes.
W.J. Gilmore has been developing PHP applications since 1997, and is frequently published on the subject within some of the Web's most popular development sites. He is the author of 'A Programmer's Introduction to PHP 4.0' (January 2001, Apress), and is the Assistant Editorial Director of Web and Open Source Technologies at Apress.
Return to the PHP DevCenter.