home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


CHAPTER 22
Cookies and State Maintenance

by Bill Chosiad and Jim O'Donnell

In this chapter
The Trouble with Stateless HTTP 540
Maintaining State 540
Cookies: An Introduction 541
Using Cookies 543
Other State Maintenance Options 554

The Trouble with Stateless HTTP

Most Web servers have very short memories. When you request a page, the server usually doesn’t really know who you are, what you entered on a form three pages ago, or whether this is your first visit to the site or your 75th. One of the challenges of using the Hypertext Transfer Protocol (HTTP) is that it doesn’t track the state of your interactions with the server. State refers to any information about you or your visit to a Web site. It is maintained as you move from page to page within the site, and it may be used by the Web server or a JavaScript program (or both) to customize your experience at the site. But if HTTP doesn’t maintain the state, what does?

This chapter shows you how to get around HTTP’s limitations by using cookies, URL query string parameters, and hidden form variables. Although the bulk of this chapter deals with cookies, time is spent investigating other techniques, as well as where and how they may best be used.

Maintaining State

Maintaining state means remembering information while the user moves from page to page within a Web site. With this information in hand, you can set user preferences, fill in default form values, track visit counts, and do many other things that make browsing easier for users and that give you more information about how your pages are used.

You can maintain state information in a number of ways:

  Store it in cookies
  Encode it in URL links
  Send it in hidden form variables
  Store it in variables in other frames
  Store it on the Web server

Be aware, however, that some technical challenges regarding state maintenance can occur. While browsing a site, a user might suddenly zoom off to another Web site and return minutes, hours, or days later, only to find that any saved state information is out of date or has been erased. He or she might return by clicking the browser’s Back button, by using a bookmark, or by typing in the URL directly, causing state information encoded in the URL to be overwritten or lost.

The Web developer must maintain state information regardless of whether the user navigates through the site using buttons on a form or a URL link on a page. This could mean adding information to both hidden form variables and every URL <A HREF...> tag that appears on the page.

With all these difficulties to overcome, these state maintenance mechanisms had better be useful. Luckily, they are. Many advantages exist to maintaining state, both within a single site visit and from one visit to the next. Consider the following scenarios:

  A shopping cart application. Users could browse through the site while selecting items and adding them to a virtual shopping cart. At any time, they can view the items in the cart, change the contents of their cart, or take the cart to the checkout counter for purchase. Keeping track of which user owns which shopping cart is essential.
  Custom home pages. Many Web sites have now set up home pages where users can customize what they see when they arrive. After giving the user a choice of layouts, color schemes, and favorite destinations, it stores the preferences on the user’s own computer through the use of cookies. The user can return to the site any time and get the previously configured page.
  Frequent visitor bonuses. By storing information on the client computer, this application keeps track of how many times a browser has hit a particular page. When the user reaches a certain level of hits, he or she gets access to more or better services.
  Change banners. You can make graphic banners and text changes each time the user hits a page. This technique is often used to cycle through a list of advertisements.
  Bookmarks. Remember where a user was when he last visited the site. Was he reading a story, filling out a questionnaire, or playing a game? Let him pick up where he left off.
  Games. Remember current or high scores. Present new challenges based on past answers and performance.

Cookies: An Introduction

Cookies—sometimes called magic cookies, but more formally known as persistent client state HTTP cookies—enable you to store information on the client browser’s computer for later retrieval. Although they have their drawbacks, cookies are the most powerful technique available for maintaining state within a Web site.

Netscape came up with the original cookie specification. There doesn’t seem to be any good reason why Netscape chose that particular name. In fact, on their cookie specification page, they even admit that “the state object is called a cookie for no compelling reason.”

In their simplest form, cookies store data in the form of name=value pairs. You, the developer, can pick any name and value combination you want. More advanced cookie features include the capability to set an expiration date and to specify what Web pages may see the cookie information.

Advantages of Cookies

One of the most powerful aspects of cookies is their persistence. When a cookie is set on the user’s browser, it may persist for days, months, or even years. This makes it easy to save user preferences and visit information and keep this information available every time the user returns to your site.

Cookies prove especially helpful when used in conjunction with JavaScript. Because JavaScript has functions for reading, adding, and editing cookies, your JavaScript programs can use them to store global information about a user as she surfs through your Web site.

Limitations of Cookies

Some limitations of cookies could prove problematic. Cookies are stored on the user’s computer, usually in a special cookie file. As with all files, this cookie file might be accidentally (or purposefully) deleted, taking all the browser’s cookie information with it. The cookie file could be write protected, thus preventing any cookies from being stored there. Browser software may impose limitations on the size and number of cookies that may be stored, and newer cookies may overwrite older ones.

Because cookies are associated with a particular browser, problems come up if users switch from one browser to another. If you usually use Netscape Navigator and have a collection of cookies, they will no longer be available for you to use if you decide to switch to Microsoft Internet Explorer.

Finally, if several people use the same computer and browser, they might find themselves using cookies that belong to someone else. The reason for this is that cookie information is stored in a file on the computer, and the browser has no way to distinguish between multiple users.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.