This month, I’m going to introduce you to some small snippets of PHP code that can be used to spruce up your Web site a little bit. PHP, which stands for Hypertext Pre Processor, is a very robust scripting language you can embed directly into your Web pages. This means that you need to have the PHP interpreter available as an Apache module (assuming you use Apache as your Web server) or the appropriate module for any other Web server you may be using. PHP is available for a variety of operating system platforms and can be obtained from the Official PHP site.
Get ready for a taunting, tantalizing tidbit of shell scripting as the one and only Vincent Danen, of MandrakeSoft, offers up his skills as a TechRepublic’s shell scripting guru! Each month Vincent will demonstrate an exciting and captivating dance with Linux script writing that will surely make the task of administrating your systems simpler and much more pleasurable. Make sure you take in all of Vincent’s previous Script Teez articles including June’s feature on automating secure remote backups, and how to uptime your e-mail from July. Be sure to tune in each month; you won’t want to miss any of these shows!
Last modification date
Most pages that you see on the Web have a last modified date, which indicates the last time the page was updated. This is good information because at a quick glance you can determine whether or not the content on a page is new (however, this date can be misleading with some dynamic Web sites). Most Web designers (okay, maybe just me) have a bit of a problem remembering to update this date every time they make a small modification to the page. This simple little PHP snippet will prevent you from having to remember to update the last modified date of a Web page ever again:
Last Updated:  
$mod = filectime(“/home/httpd/html/index.php”);
echo date(“m/d/y”, $mod);

Simply replace the path and filename, depending on which page you are using. An easy way to keep consistency among your pages is to use include() statements. For example, you can write a common header or footer to every page in a single file, and each page on your Web site can simply include the page. For instance, if you were to include a header, let’s say header.php, on every page, you could rewrite the above piece of code and use it in the header as opposed to an individual page.

You’re probably asking yourself why you might want to do this. Let’s take, for instance, this scenario: Most Linux systems use the /home/httpd/html directory as the base for serving Web pages with Apache. According to the FHS (Filesystem Hierarchy Standard), these pages should actually be in the /var/www directory. You initially design your site using /home/httpd/html for your site, and you have over a hundred separate Web pages that you serve from your site.

You can either reconfigure Apache to use /home/httpd/html again, you can comply with the FHS and move everything to /var/www, or you can create a symbolic link between the two directories. For the sake of this example, let’s assume that you wish to remain compliant with the FHS and move everything to /var/www. You now have 100 pages to update, individually, to point to the new directory structure. Ouch!

You can avoid this problem by placing the following code in your header.php file:
<? $mod = filectime(“/home/httpd/html/$page”); ?>

Then you’d use this piece of PHP code at the top of your individual Web pages:
$page = ?index.php?;
include (?header.php”);

The first thing you do is define the variable $page, which is simply the name of the Web page you’re dealing with, in this case index.php. Then you tell PHP to include the header.php file. Now when header.php is loaded, it uses the value of $page to determine which filename to find the last modified time of.

What if you’re in a subdirectory? That’s easy to deal with. Just use relative directories, like this:
   $page = ?subdir/mypage.php?;
   include (?../header.php?);

With this method, $page will properly expand to /home/httpd/html/subdir/mypage.php when you include header.php in the directory one level up. See how simple it is if you want to move everything from /home/httpd/html to /var/www? Just one page needs to be changed, instead of a hundred.

Then, just include the following piece of code in your Web page wherever you want the last modified date to be displayed:
<? echo date(“m/d/y”, $mod); ?>

Of course, you can change the date() function to display the last modified date however you like. In this case, display it as mm/dd/yy.

You can do the same for the footer of your page by placing the information you want to be displayed on each page into the file footer.php and using the following as the last line of code in your Web pages:
<? include (?footer.php?); ?>

You now have dynamically included headers and footers for each page. If you want to change the copyright information on your site from one year to the next, you can easily accomplish this by making one change to footer.php. The resulting change is made globally across your site.

Reading and displaying filenames from a directory
Another thing you may wish to do on your Web page is list the contents of a directory for download. Let’s take, for example, a directory of software packages you have available for download. These packages change on a relatively constant basis since you’re a very enthusiastic programmer. You’d much rather spend your time programming than maintaining your Web site, so having an easy way to list the current contents of a directory and auto-generate an FTP link to the file is more to your liking than modifying the Web page each time you update a file.

This technique is relatively simple to implement with a little bit of work. I’ll also throw a little twist into the mix: You want to make the visitors to your page aware of the last time the files were modified. So you’re not only generating a list of FTP links based on the contents of a directory, but you’re also informing the user of when you last updated the file. With a little creative PHP code, this is very easy to accomplish. For this example, I’ll assume that you release your packages in RPM format. Simply include the following in your Web page:
Downloadable as:<br />
<?….$open = “/home/ftp/pub/software”;
   $handle = opendir($open);
   while ($file = readdir($handle)) {
   if (ereg( “.rpm$”, $file )) {
        if (ereg( “.src.rpm$”, $file)) {
          $mod = filemtime(“/home/ftp/pub/software/$file”);
        <li> <a href=”
        <? echo $file; ?>”><? echo $file; ?>
        </a><br />
     <? } else {
<hr /><br /><br />
<b>Files last modified:
<? echo date(“D, M d Y H:i”, $mod); ?>
</b><br /><br /><br />

Now, what does this elegant little piece of mixed HTML and PHP perform? The first thing you do is define the $open variable and point it to the local directory where the software is stored, which is /home/ftp/pub/software because /home/ftp is the root directory for your FTP server. The next thing you do is open the directory with the opendir() function and assign to it the file handle $handle.

Next, enter a while() loop. This loop reads the contents of the directory and terminates when there are no files left to read. You assign each file found to the $file variable. The next line, which is the first if() statement, performs a regular expression against $file to determine if it is a file that ends with .rpm. Remember, all of your software is released in RPM format, so you want to display only the RPM files on the site.

If the file is an RPM file, you do another regular expression match against it to see if it ends with .src.rpm, which is used to store the source code of the program. You are writing open source software, right? At any rate, if this matches, you assign the last modification date of the file to the variable $mod. After this, you display the link on the Web page. As you can see, it is a normal FTP link pointing to the FTP site and the appropriate directory. You then echo the contents of the $file variable (which is your filename) to the link twice. The first time is to complete the href tag, and the second is to display the filename to the user.

Finally, if the file is not an RPM, you do nothing with it and continue your while() loop. And once you have listed all of the RPM files in the directory and exited the loop, you display a line of text that indicates what date the source RPM was last modified.

PHP is an extremely flexible and powerful Web scripting language that more and more people are incorporating daily into their Web sites. It is very fast, which makes it ideal for serving Web pages as it does not create any noticeable delay from when a visitor requests a page to when the page is served to them. And as I’ve just illustrated, PHP can be a great time-saver. The time it takes to initially write the PHP code for your pages is minimal compared to the time you save in manually updating your Web pages with information you really shouldn’t have to update. This article introduced you to two simple little scripts to help make your life a little easier, so take the time you save not updating your Web pages to relax and enjoy that coffee.

Vincent Danen, a native Canadian in Edmonton, Alberta, has been computing since the age of 10, and he’s been using Linux for nearly two years. Prior to that, he used OS/2 exclusively for approximately four years. Vincent is a firm believer in the philosophy behind the Linux “revolution,” and he attempts to contribute to the Linux cause in as many ways as possible—from his FreezerBurn Web site to building and submitting custom RPMs for the Linux Mandrake project. Vincent also has obtained his Linux Administrator certification from Brainbench .He hopes to tackle the RHCE once it can be taken in Canada.

The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.