Web Development

Use network protocols with PHP

With the ever-growing complexity of Web sites, Web administrators need as much help as they can get with processing content. PHP's built-in network protocol commands are perfect for the job, and Vincent Danen is here to explain them.

With the increasing complexity of Web sites and the potential for automated interaction between sites, you may find yourself in a situation where you want to retrieve remote data to include in your site. You can easily accomplish this with PHP by making use of its built-in network protocol commands. With these commands, you can have a Web page act as a front end to upload and download files via FTP or retrieve information from another Web site using HTTP, among other useful functions. Let's take a look at some of these commands.

First, we'll examine the mail() function, which allows you to send e-mail messages from your Web site using SMTP. This is the prototype for the mail() function. It is extremely straightforward and gives us a very simple means of sending e-mail messages.

An example PHP script to send the e-mail might look like this.

You don't need to assign variables for everything; you could easily have one huge mail() function call, but this method makes it easier to read. And, of course, if you are using dynamic information, you’ll want to use variables. This could be used to make a very simple and efficient Web feedback form. You could hardcode the $toaddress and let the $content, $subject, and $fromaddress variables be retrieved from the form.

Next, you can use PHP to retrieve data from other Web sites. You have most likely used the fopen() command to open local files before, but did you know that it can also open remote Web sites? Instead of specifying a local file, specify the full URL you are interested in. For example, you could use this script.

Here, we have defined the value of $url as a fictional Web site with some operating parameters. This illustrates that you can use some special characters, like the ampersand (&) or the question mark (?) for pages, if you need to. Next, we open the URL with the fopen() function, assigning the file handle to the variable $fp. We wrap this in an if() statement so that if it cannot negotiate a connection, we can immediately exit the script with our error message.

Next, we use the fread() function to read in the file handle $fp. We use a huge number with fread() to tell PHP how much information to read into our variable $pcontent. This is necessary because there is nothing like the filesize() function for URLs; you will not be able to determine the exact size of the remote page you are opening automatically. You may need to make this number higher if you are opening a very large Web page.

Finally, we close the file handle, which terminates the connection to the remote Web site. We then print the contents of $pcontent. Ideally, you would use some processing on $pcontent to retrieve only the information you are interested in (instead of displaying the entire page), but this illustrates how to open connections to remote Web sites and retrieve information.

Another handy function is gethostbyname(), which will allow you to determine whether or not a given URL is valid. This could be particularly useful for Web sites with link sections when you want to determine whether the URL actually exists. The following code will check to see if the variable $url (a user-submitted variable via a form) is valid:
<?
  $url = parse_url($url);
  $host = $url['host'];
  if (!($ipaddr = gethostbyname($host))) {
    print("URL does not have a valid IP address.");
    exit;
  }
  print("URL is at the IP address: " . $ipaddr);
?>


First, we run the parse_url() function against the user-submitted URL. This function returns an associative array of the different parts of the URL; it returns the scheme, user, pass, host, port, path, query, and fragment. Not all URLs will return each part of the array, but a URL with the full number of elements might resemble this one.

If this were the URL submitted by the user, the resulting array would look like this:
$url['scheme']   = http://
$url['user']     = "user"
$url['pass']     = "pass"
$url['host']     = "mydomain.com"
$url['port']     = "80"
$url['path']     = "index.php"
$url['query']    = "section=02"
$url['fragment'] = "bottom"


As you can see, all we are interested in for this particular use is $url['host'], which is the hostname of the remote Web site. Next, we use the gethostbyname() function against the hostname, which will return the IP address if there is one, or false, if there isn't. You can also work the other way by using the gethostbyaddr() function, which will return the hostname of a given IP address.

You can also determine whether a supplied e-mail address is valid by checking to see if the MX record exists for the domain name, using the getmxrr() function. A sample code fragment might look like this, where $email is the e-mail address submitted by a user in a form.

This takes the user-supplied e-mail address (stored in the variable $email), and splits it into the username and domain name. We assign to the variable $host the domain name and run the getmxrr() function against it to retrieve the MX records from DNS for that domain name. We assign the returned addresses to the array $mx. If there are one or more valid MX records, we loop through them and temporarily assign each MX record to the variable $x and print it in a list.

Conclusion
As you can see, there are a number of checks you can include in your scripts to automatically determine whether a submitted value is valid. Making use of these functions can help you reject those submissions that are flat-out false. Using the other functions can add a new level of interaction to your Web site.

Next time, we'll take a look at using some more network connectivity in PHP, primarily by using the built-in FTP functions and handling HTTP uploads to your Web site.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

0 comments