Saving state

The WEB was originally designed (the HTTP protocol) to deliver documents. The conversation needed between client and server to accomplish this is simple:

CLIENT: GET me this document.

SERVER: HERE it is.

End of story.

There is no need to have any memory of previous conversations - so the interactive aspect of the WEB is absent. As the WEB developed there was an increasing demand for the facility to interact, and in order to do this, past conversations needed to be remembered - this is what is meant by ``saving state''.

We will discuss 2 ways of saving state. For the first method the state information is stored on the server, and for the second it's stored on the client.

Session keys

For this method, a database sits on the server. Each user provides a key into the database, the script takes the users key and uses it to look up their information on the database. This information can be modified and added to. The key is usually passed between client and server in a hidden field in the HTML form. This way, when entered once, the user no longer sees it.

Cookies

Cookies are good ways of passing around small amounts of information. They reside on the client. A cookie in its most basic form consists of a key/value pair together with a URL. Whenever you send a GET request to a URL, your browser checks the cookie data base to see if any cookies belong to that URL. If they do, then the cookies are added to the HTTP header. The server will then receive the GET request together with the cookies which can then be processed as desired - usually to extract some information about you.

A basic cookie setting command looks like this and would be sent by the server in the HTTP header:

Set-Cookie: name=Waterman

This could then be stored on the client data base as

www-stat.wharton.upenn.edu/~waterman name=Waterman

If the client returns to www-stat, and requests a document in the waterman subdirectory the following line is appended to the GET request:

Cookie: name=Waterman

Here's a simple script that sends out a cookie.

#!/usr/local/bin/perl

# A script that sends out a cookie.
# The cookie is sent by adding a line to the HTTP header.

print ``Content-type: text/html\r\n'';
print ``Set-Cookie: name=Waterman; path = /cgi-bin/\r\n'';
print ``\r\n'';

### Now print out the HTML document.

print ``<HTML>'';
print ``<HEAD><TITLE> Cookie sender</TITLE></HEAD>'';
print ``I've just sent you a cookie!'';
print ``</HTML>'';

Cookies have options that can be set;


next up previous
Next: Using mod CGI Up: Class 3 Stat 540 Previous: Reading fromand writing

Richard Waterman
Fri Jan 29 06:45:16 EST 1999