Download CD Content
The goal of this chapter is to describe the use and application of cookies and session variables.
The design of the original World Wide Web was to support sharing of information among scientists using what are termed stateless protocols. The browser program on the client computer sends requests to the server software on the server computer. Each request was and is handled independently. The server software does not “know” (the “know” is in quotes as an excuse for using such an anthropomorphic term) if a request is from a client computer that has made a request recently or has never made a request before this one. Similarly, no information entered or calculated about the particular site visitor is retained from previous visits or passed on to another Web page on the same site. This design simplifies the workings of the Web, but does not support the requirements of complex applications such as e-commerce.
Here are two examples of situations in which the site builders would want to retain information.
An online application might involve multiple Web pages. One example of this is online shopping. The customers move from catalog pages to specifying details of the order, to catalog pages again, and then to the check-out page. The technical term for this is transaction. The system designer wants to maintain all the information and then execute the complete transaction. For these situations, the information needs to persist for the duration of the time the visitor is seeing the pages belonging to the application.
Many sites require the visitor to log on to the system. Since most of us “belong” in this way to many sites, this means remembering multiple sets of logon ID and passwords. The owners of Web sites would like to simplify the process for their clients. As a gesture of good will, in addition, the owners want to greet each visitor with a personalized welcome. This greeting could, of course, include a customized marketing appeal. For this situation, the site owners want to access the information each time a visitor returns to one of the Web pages on the site.
Cookies were developed to address these types of situations. The term cookies is intended to be a term of innocence and fun. Cookies are small files located on the client computer. These files are written by a specific Web site and used only by code on that Web site. They last for the duration of an invocation of a browser or for a fixed amount of time, which could be days or months.
Session variables are more elaborate mechanisms to hold information over the course of a visit to one or more Web pages located on a single server. The bulk of the session data is kept on the server computer.