Internet

Beginning CGI Programming with Perl

Using the fsize Command

The fsize command is used to insert the size of a file into your Web page. Remember that the fsize command can operate on any file-the file the SSI command is in or some other file.

This really works great when you have a Web page with a lot of images on it. Instead of putting many large images on your main page (something that I find really irritating when surfing around the Net), you can include thumbnails of each of your images on your home page. Then, beside each thumbnail image, use the fsize command to indicate how large the full-sized image is. This speeds up the loading of your Web page. First, this means that more people will wait to see what is on your Web page. Next, it lets your Web page visitor decide whether she wants to spend the time downloading the larger images. This always is considered proper etiquette on the Net. Your Web site will be a lot more successful if you use this technique.

The fsize command has basically the same syntax as the include and flastmod commands. It accepts two command arguments: virtual and file. And virtual and file have exactly the same meaning for fsize as they do for flastmod and include. The virtual command argument defines the path to the file from the document root, and the file command argument defines the path to the file relative to the current directory.

Using the echo Command

SSI commands are designed to make your Web tasks easier. Sometimes, when dealing with UNIX and programming, life can get pretty frustrating. The smallest error makes everything not work. SSI commands can seem like that sometimes. When you forget to leave a space before the closing SSI command HTML tag (-->), or when you add a space between the hash sign (#) and the SSI command (<!--# echo), nothing works and you get that silly and ever-so-helpful error message

["an error occurred while processing this directive"]

That’s a lot of help!

Well, whoever wrote the code for the echo SSI command took pity on us poor, imprecise humans. Can you believe it? The five variables you can print using the echo command are not, I repeat not, case sensitive! I bet you just opened a bottle of champagne and are dancing around the room right now. Well, sit down and get back to work; you’re just getting started, and this reprieve from case sensitivity only lasts for a few paragraphs. Just wait until you get to the exec command. Then you’re in for it!

As I stated in the last paragraph, five variables can be used with the echo command; these are summarized in Table 3.4. “Why only five?” you ask. It does seem kind of weak, doesn’t it? Well, I don’t really know the answer, but it actually makes a lot of sense. Remember that SSI commands are designed to include other files and to enable you to do a bit of dynamic Web page work. (That’s creating Web pages on-the-fly, in Net slang.) These variables are the minimum set of variables you need to describe files you are including and to give you current information about the main file. Why not provide more? Well, the more you get, the more complex things become. Very quickly, you might as well write a CGI program and forget about SSI commands altogether. And for the most part, you will. But SSI commands are very handy to have around, mainly because of their lack of complexity.

Table 3.4. The echo command variables.

Variable Specifies
Date_GMT Current date and time in Greenwich Mean Time. Greenwich is used by the entire Net as a common time for communications purposes. Because you can never tell who will be using your Web page, this time format makes a lot of sense.
Date_Local Current date and time in the local time zone. The time zone is determined by the location of the server and the server’s software. The format is visible in Figure 3.5. The output of this command is configurable by the SSI command config timefmt.
Document_Name Filename of the main document.
Document_URI Pathname and filename of the main document.
Last_Modified Date and time the main document was modified.

Figure 3.5 : Using the SSI echo command.

Figure 3.5 shows the use of each of the variables available to the echo command. Notice at the end of the first line the word (none). This is what happens when you try to echo an invalid variable. Because the echo command can’t see the variable, it prints (none), just as if you had asked it to echo nothing (which, as far as the echo command is concerned, you have).

Listing 3.6 shows the HTML and SSI commands used to print these variables. Most of this syntax is very similar to the other SSI commands, and therefore is self-explanatory. But, as always, you should be aware of at least one trick. Notice the different dates on the last few lines in Figure 3.5. When you include files that use the echo command, the variables the echo command uses are the ones defined by the main file. So the Last_Modified, Document_Name, and Document_URI variables all refer to the first file parsed by the server.

Why does this happen? Well, all the global variables available to this process are set when the process is started. The first file opened by the server defines the environment under which all the other files will operate. The variables the echo command refers to are set when the server opens the first file for parsing. These variables are not set again, regardless of how many new files the server might need to include in the first file. Listing 3.7 shows the small include file included on line 15 of Listing 3.6. Notice that the first line prints the Last_Modified variable, which still refers to the first file opened for parsing. The last line of Listing 3.7 refers to itself and gives the date you would expect Last_Modified to print when echoed.


Listing 3.6. HTML and the SSI echo command.

01: <html>02: <head><title>The server side include echo command</title></head>03: <body>04: <h3> When you try to echo something that is invalid05: you get the following error message:06: <!--#echo var="$env" --></h3>07: <h3>This is the name of the document the echo command is in08: <!--#echo var="DOCUMENT_NAME" --></h3>09:10: The path to this file is   <!--#echo var="DOCUMENT_uri" -->.<br>11: The current local time is  <!--#echo var="DATE_LOCAL" -->.<br>12: The Greenwich Mean Time is <!--#echo var="DATE_GMT" -->.<br>13: This file was last modified on <!--#echo var="last_modified" -->.<br>14:15: If you include a file that has the echo commands in it16: all of the echo commands refer to the main file.<br>17: <!--#include file="server side include_last_mod.shtml" -->18: </body>19: </html>

Listing 3.7. An include file using the SSI echo command.

1: The <!--#echo var="DOCUMENT_NAME" --> file was last modified.2: <!--#echo var="LAST_MODIFIED" -->.<br>

The Syntax of the SSI echo Command

The syntax of the echo command follows the SSI command syntax, of course:

<!--#command cmd_argument="argument_value" -->

The command argument is var, and the argument values are the variables listed in Table 3.4. The exact syntax is shown on lines 8-13 of Listing 3.6. Remember that, with this command, the variables of the argument_value field are not case sensitive. Document_Name is the same as DOCUMENT_NAME, for example.