next up previous
Next: About this document Up: No Title Previous: No Title

Lab Assignment #4

The due date is 11:30 a.m. on Friday, February 12. Because this is being handed out only five days in advance, if you need more time check with me individually beforehand. Get started soon in any case, because you'll be able to follow the lectures much better. There is actually less to do in this assignment than may appear.

From now on, please hand in all homework files, including HTML and CGI scripts, using submit . Of course, you'll still need to test HTML files and CGI scripts by putting them in public_html and viewing them.

1. More HTML

Fix up your home page index.html so that it contains all of the following features. Then view it to check it. Then submit it.

For a table,

Here is an example of a table. A couple of optional ``attributes'' that you can use are included in the opening tag. (The alignment tells where to put the table across the page. The border thickness can be 0, 1, 2,...)

<TABLE BORDER=1 ALIGN=CENTER>
<TR>
        <TD> If </TD>
        <TD> you're </TD>
        <TD> able </TD>
<TR>
        <TD> use  </TD>
        <TD> a </TD>
        <TD> table. </TD>
</TABLE>

2. A helpful routine

Make, test, and submit a Perl script called cgi_helper that contains subroutines to output the appropriate header for a CGI program, and also the appropriate ending HTML tags. Details:

3. The CGI environment

When a UNIX program runs, there are actually four ways it can get information from outside itself. You have seen the first three.

  1. from arguments
  2. from reading standard input
  3. from opening a named file for reading and then reading it
  4. from environmental variables

Environmental variables are set by the system before the program runs. As a warmup, try the UNIX command

printenv and see what you get. Some environmental variables you will understand, others not. In each programming language there is some way for a program to read these variables. (It is also possible to invent and set more environmental variables yourself, but we won't need that.)

In Perl, it is very easy to get the values of environmental variables; they are in a hash named %ENV that is already set when you run the script. You don't need to know in advance which ones are defined since you can get that from keys %ENV . As you know, the keys may come out in any order but you can always sort them if you want a nicer order. Also as you know, if one of the environmental variables is the string ID, then its value is $ENV{"ID"} or if $evar is an unspecified environmental variable name, then its value is $ENV{$evar} .

Now make, test, and submit a Perl script environ.cgi that

When you run this script from the UNIX command line, you should see the same variables as before.

However, when you access this script through your browser using its URL, you will see a different list of environmental variables. These are supplied by the Apache web server software. Later these variables will help to get information from web forms filled in by users.

4. An access counter

Make, test, and submit a CGI script counter.cgi that counts the number of times it has been called and shows the result on a web page. This is a ``naive'' version that has some defects to be fixed in a more sophisticated future version.

A CGI program can't remember information by itself, so the current count has to be kept in a file. The file can't be standard input, since the CGI program is run by the Apache web server software and not on a command line by you. Call the count file access_count.dat . (The .dat doesn't mean anything in UNIX but it suggests data when you see it listed among other files.)

Your script should

You can first test this program by running it on its own. But you should also be able to access it through your browser, with the appropriate URL, and it should keep count. To force a new access, click on the browser ``RELOAD'' button. If you return to the page after browsing elsewhere, again click the ``RELOAD'' button; otherwise you may see an older cached copy of the page.

The weakness in this program is that two different users might access it at the same time and compete for reading and writing the count file. One user might get frozen out, or the file might even be damaged. Our future version will ``lock'' the file so that only one user at a time can access it.


next up previous
Next: About this document Up: No Title Previous: No Title

Kirby A. Baker
Wed Feb 10 05:47:22 PST 1999