CSC 153 Grinnell College Spring, 2005
 
Computer Science Fundamentals
Laboratory Exercise
 

CGI Programming

Goals

The lab discusses some basic elements of CGI programming, which allows a Web developer to tailor documents to an individual Web user.

CGI Scripts

  1. Run the http://www.walker.cs.grinnell.edu/cgi-bin/what-i-know-script.cgi script, to see what is printed.

  2. Copy the what-i-know-script.cgi script and the Scheme program what-i-know-script.ss to your account, and share each program. Then try running your copy of the script from your Web browser.

  3. Add comments to the Scheme program what-i-know-script.ss to annotate what each section of code is doing.

  4. Add a couple lines at the end of the program, so that the program displays your name at the bottom of the Web page it produces.

Query Strings

Query strings represent a mechanism for a browser to pass information along to the Web server for use in a CGI script and program. Frequently, Web pages include an html device, called a form for this purpose, but the same information may be included directly in a URL by typing a question mark ? and then adding the relevant data.

The following exercises utilize a directory of the 1998-1999 faculty in the Mathematics and Computer Science Department at Grinnell College.

  1. Fill in the requested information in http://www.walker.cs.grinnell.edu/cgi-bin/fac-directory.html, to determine information about Henry Walker and about John Stone. (Also, look under Nathaniel Borenstein and Pamela Ferguson -- who shared an office during the 1998-1999 academic year.)

  2. To view this form itself, click on the above document and then ask your browser to view the page source. There, you will see that the submit input will result in the action of requesting the CGI script fac-directory.cgi. Similarly, you will find the labels and field names for each input textual field.

When Henry is entered into the First Name box and Walker is entered into the Last Name box, then when the user clicks the submit button, a request is generated to the Web server -- as shown in step B above.

  1. Instead of using this html form, type the line http://www.walker.cs.grinnell.edu/cgi-bin/fac-directory.cgi?firstname=Henry&lastname=Walker as the URL directly into your browser. Note that the result is exactly the same as using the form. That is, the html form within fac-directory.html simply provides a convenient way to generate URL addresses with query string information added.

  2. Modify the what-i-know-script.ss program, which you annotated and expanded earlier in this lab, to display any query string which is included in a URL to the script what-i-know-script.cgi. Thus, if you typed the URL

    
    http://www.cs.grinnell.edu/~yourUsername/cgi-bin/what-i-know-script.cgi?IlikePOPCORN!
    
    
    the program should include the following text in its output
    
    IlikePOPCORN!
    
    

In CGI programming, a query string usually consists of a sequence of equations separated by ampersands, with some attribute on the left-hand side of each equation and the value of that attribute on the right-hand side. For instance, in our form example, the query string had the form firstname=Henry&lastname=Walker. As noted in the reading for this lab, it is common to decode query strings to yield an association list of field names and associated values. The library routine extract-attributes procedure in http://www.walker.cs.grinnell.edu/public_html/cgi-bin/cgi-utilities.scm accomplishes this task. This Scheme code, written by John Stone, takes a query string as argument and returns a list of pairs, with the car in each pair being a fully decoded attribute and the corresponding cdr being its fully decoded value:

  1. Copy the html form ~walker/public_html/cgi-bin/fac-directory.html , the script ~walker/public_html/cgi-bin/fac-directory.cgi script and the Scheme program ~walker/public_html/cgi-bin/fac-directory.ss to your account, and share each file (chmod 755). Then try running your copies of the html form and script from your Web browser.

  2. Review the source of the program fac-directory.ss. Then discuss (in paragraph form) how the program works.

  3. Modify the program fac-directory.ss to allow only the partial specification of a name. In particular, if a user enters both a first and last name (in fac-directory.html), then the program will respond in its current way. However, if the user enters only a last name, then the program will return all people who have the given last name.

  4. Modify the interface fac-directory.html and the program fac-directory.ss to retrieve all people with a given telephone number. That is, fac-directory.html should be revised so that it asks the user for a telephone number; program fac-directory.ss then should return all entries in the directory which match that number. (Note, this reverse lookup is very common in various Web-based directories.)

Work to be turned in:

For steps 3, 4, 8, 11, and 12, only a printout of the relevant files are needed. You need not show a run of the program (as any run will show up in your Web browser rather than in a window that you can record).


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/lab-cgi-programming.shtml

created November 3, 1998
last revised March 7, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.