Next / Previous / Contents / TCC Help System / NM Tech homepage

7. reader.cgi: An example script with cookies

In this section, we present a complete CGI application that uses cookies. It is a skeletal form of the reader for the Great American Novel described in Section 6, “Cookies: Do I know you?”.

This script is not designed to be a polished product. Rather than presenting actual chapters of text, we'll display just the sentence “This is chapter N.” The intent of the script is to show how cookies work in practice.

Here are the navigational features on the page:

Here's a screen shot of the page on entry:

And the XHTML behind this page:

mockread.html
<html xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>The Great American Novel reader</title>
  </head>
  <body>
    <h1>The Great American Novel reader</h1>
    <form method='get'
          action='http://infohost5.nmt.edu/~tcc/demo/pycgi/anyform.cgi'>
      <div>
        <input type='submit' name='next' value='Next chapter'/>
        <input type='submit' name='prev' value='Previous chapter'/>
      </div>
      <div>
        This is chapter 1.
      </div>
      <hr/>
      <div>
        <input type='submit' name='next' value='Next chapter'/>
        <input type='submit' name='prev' value='Previous chapter'/>
      </div>
      <div>
         <input type='radio' name='request' value='s' id='req-b'
               checked='checked'/>
        <label for='req-b'>I'll be back (remember me until my
        browser session ends)</label>
      </div>
      <div>
        <input type='radio' name='request' value='p' id='req-p'/>
        <label for='req-p'>Remember me (for a week)</label>
      </div>
      <div>
        <input type='submit' name='erase' value='Forget me'/>
      </div>
    </form>
  </body>
</html>

When the user is just reading their way through the chapters, the above page layout appears.

When the user clicks the Forget me button, however, the displayed page is a short announcement of success in erasing the user's cookie:

mockerase.html
<html xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>The Great American Novel reader</title>
  </head>
  <body>
    <h1>The Great American Novel Reader</h1>
    <div>
      Your records on this server have been erased.
    </div>
  </body>
</html>

Click here to try the page yourself.

7.1. Design notes for reader.cgi: Sequencing

For reasons discussed in Section 6.1, “Design considerations for CGI with cookies”, this script must carry out these three phases in sequence:

  1. Gather all input. Most of the input comes from the FieldStorage instance containing the name-value pairs from the form. However, an incoming cookie, if any, comes from an environmental variable.

  2. We must write the header or headers to standard output before we start generating HTML. A header line that sets or deletes a cookie must come first. In any case, the last header must be the standard “Content-type: text/html”, followed by a blank line.

  3. There are two cases of HTML generation. If the user has just deleted their cookie, we'll generate the short confirmation page. Otherwise, we generate the general-purpose page, containing the requested chapter for this user.