PHP has a great many tools for interacting with a network and also with the Internet. In this article, I examine some of those tools and functions to show how exactly you can use them to make your scripts more useful in a network environment. Click here to download the accompanying source code.
Accessing Other Websites with PHP
Accessing other sites with PHP is extremely easy, but why would you want to do that? If you just want to get information from a website (for instance, a website that offers weather reports for a city that is of interest to you), then you can write a spider to get that information and more.
In PHP, you access a website in pretty much the same way that you would access a text file on your hard drive — by using
fopen()
.
fopen(http://localhost/websecure/ftp.php, "r");
The
fopen()
function that you use to open files is the same one you use to open Web pages, because a Web page is essentially a file on a server. The fopen()
method has the modes listed in the following table.Table 1. Modes of fopen() Method
Mode | Meaning |
---|---|
r | Reading only — Begins reading at the start of the file |
r+ | Reading or writing — Begins reading at the start of the file |
w | Writing only — Creates the file if it does not exist, and overwrites any existing contents |
w+ | Reading or writing — Creates the file if it does not exist, and overwrites any existing contents (when writing) |
a | Writing only — Creates the file if it does not exist, and appends the new data to the end of the file |
a+ | Reading or writing — Creates the file if it does not exist, and overwrites any existing contents (when writing) |
x | Writing only — Creates the file if it does not exist, but does nothing (issues a warning) when the file does exist |
x+ | Reading or writing — Creates the file if it does not exist, but does nothing (issues a warning) when the file does exist |
You will be able to open a file only for reading, unless the permissions are set otherwise. Also, you will have to use a trailing slash after a directory because
fopen
does not support redirects. For example, this example is fine:
fopen("http://localhost/websecure/ftp.php/", "r");
But this one will fail:
fopen(http://localhost/websecure/ftp.php, "r");
When you’ve opened a file, you can pretty much treat it the same way that you would any other file, using common functions such as
file()
or fgets()
to place or retrieve the data.Example code might be something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="tester.php" method="get">
<input name="url" type="text" />
<input name="" type="button" />
</form>
</body>
</html>
And the processing code might look something like this:
<?
$url=$_POST['url'];
$fp=file($url);
$n=count($fp);
$r=rand(0,($n-1));
echo trim($fp[$r]);
echo $n;
?>
DNS and PHP
PHP provides a powerful set of functions for DNS name resolution. The functions that PHP provides are limited to DNS client functionality; it provides no server-related functions. However, the DNS client functions are enough because most applications will require only 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 — 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 you 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 on to 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. Find more information on BIND here.
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 the script was run on).
While each machine on the TCP/IP network must have at least one IP address, there is no requirement that it should have a DNS name. Thus, it is entirely possible that a machine may have an IP address and no DNS entry. In which case, the function returns the
ip_address
argument itself. In fact, if an error occurs, the return value of the function is the ip_address
argument.
getprotobyname()
int getprotobyname(string name)
This function returns the protocol number associated with a TCP/IP protocol, which is a unique integer associated with the protocol name that is passed to it as an argument. The name to protocol mapping is stored in a text file, usually in /etc/protocols on UNIX-derived systems and %SystemRoot%System32driversetcprotocol on Microsoft Windows NT or Windows 2000.
getprotobynumber()
string getprotobynumber(int number)
This function has just the opposite functionality of the
getprotobyname()
function in that, given the protocol number as an argument, it displays the protocol name.
getservbyname()
int getservbyname(string service, string protocol)
We need to obtain a well-known port number at which a service is running before our client can connect to it. This function takes a service name and the transport protocol (that is, either TCP or UDP) and returns the port number associated with the service. On most UNIX machines, the port number to service mapping is specified in the /etc/services file:
<?php
$protocol = "smtp";
$portNum = getservbyname($protocol, "tcp");
echo("The port number of the $protocol service is $portNum");
?>
This prints the port number of the Simple Mail Transfer Protocol (SMTP) as 25. On most UNIX machines, the information mapping the protocol name with the protocol number is available in the /etc/protocol file.
getservbyport()
string getservbyport(int port, string protocol)
This function has just the opposite functionality of
getservbyname()
; it prints the service name associated with the port number argument. The protocol argument indicates the transport protocol (that is TCP or UDP) that the service is implemented over.As we saw, the
getprotobyname()
, getprotobynumber()
, getservbyname()
, and getservbynumber()
functions obtain their information from files on the local file system (on most platforms). Therefore, they are not really DNS functions, but I examine them in this context because they are essentially resolver functions.