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:
A button and a button, so the user can move in both directions among the chapters.
These two buttons appear twice: once above the chapter text, and once below. This may seem silly, but in a production application, there might be quite a bit of text between them.
A set of two radiobuttons that the user can select to control their cookie:
If they select (the default), they will get a browser cookie, which will go away when their browser session ends.
If they select , they'll get a persistent cookie that expires in a week.
If the user clicks a button labeled , their cookie will be deleted.
Here's a screen shot of the page on entry:

And the XHTML behind this page:
<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 button, however, the displayed page is a short announcement of success in erasing the user's cookie:
<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.
For reasons discussed in Section 6.1, “Design considerations for CGI with cookies”, this script must carry out these three phases in sequence:
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.
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.
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.