Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 20. Implementing Cookies - Pg. 382

382 n Chapter 20 Chapter 20 Implementing Cookies Maintaining a State When you create a web site, you expect the user to load HTML documents, view them, navigate from one page to another, etc. Occasionally, it is impor- tant to enable the web page to maintain a state. That is, the page "remem- bers" certain actions executed by the user during previous sessions. A classic example of maintaining a given state is a shopping cart applica- tion, as you might see on almost any commercial web site. The user travels from one product review to another via simple HTML links. When he or she comes across an interesting product, clicking a button puts the selected product's data in a "shopping cart." The shopping cart, which is sometimes displayed visually on the page, is basically a name for a storage mechanism. Since it is not possible to store the data for each user on the server, the data is kept on the client side, in what is called a shopping cart. Cookies are a general mechanism that server-side applications (such as CGI) and client-side JavaScript scripts can use to store textual data on the client side for the purpose of retrieving it later. The name "cookies" does not have any technically significant meaning. It is simply a name that the inventors thought was cool. You probably are aware that cookies have gained a bad reputation in recent years. Some unscrupulous web site developers have created cookies to store information that many people feel should be private. A common tactic for legitimate web developers is to put a policy statement on their web sites stating exactly what the site stores in cookies and why it does so, which may help alleviate customer concerns. As you will see in this chapter, cookies are a very useful tool. Cookies are tidbits of information, stored in a browser-dependent format on the client machine. Netscape Navigator, for example, holds all cookies in a regular text file named cookies.txt (in the directory where Navigator is installed), whereas Internet Explorer stores cookies in multiple files, located in a user-provided directory.