So, you finally got your new computer up and running. You fire it up, connect to the Internet, and start surfing the Web. You start by searching for all of the things you are interested in: classic cars, motorcycles, the Minnesota Vikings, and, of course, computers. Then, you decide to check out one of those sites youve heard about that sell books. You key in the URL (youve got it memorized from all the commercials youve seen).
Suddenly, a dialog box pops up on your screen. It reads, accept cookie? Youre stunned. What the heck is a cookie? And why didnt it offer me milk as well?
If you have experienced this, you most likely have asked the same questions. A cookie, simply put, is a piece of textual data that is stored in a file on the clients (the person surfing the Web) machine. Cookies are placed on the clients machine at the request of the server (the host of the site you are visiting) to store information that is specific to the client. Note that cookies contain only text. Cookies contain no binary data, so you can forget about the possibility of viruses getting on your PC by allowing cookies to reside on your machine. A cookie is generally a very small file containing only a few bytes of information about the client. If you want to take a look at the cookies on your machine, Microsoft Internet Explorer stores cookies as individual text files in a directory called Cookies in your Windows directory. Netscape stores all cookies in a single file called cookies.txt under the Users directory within the Netscape directory. For more information on cookies, visit info.internet.isi.edu/in-notes/rfc/ files/rfc2109.txt. For more information on the security of cookies, visit www.ciac.org/ciac/bulletins/i-034.shtml.
One useful tool that cookies can provide is a shopping basket. When you are surfing a site and clicking on items that you wish to purchase, information such as item number, quantity, and price can be stored in a cookie on the clients machine. This makes it easier for the site to keep track of what each user is purchasing and makes shopping faster for everyone. Just imagine if a site had hundreds of people shopping at one time. The processing taking place if the order information were stored on the server would most likely slow the site to a crawl and could cause a disk storage problem. But, by moving the processing and storage to the clients machine, the shopping experience becomes much more enjoyable, as the server is able to direct its processing and storage to building and displaying Web pages.
The first function, WriteCookie, accepts three parameters. The first is the name of the cookie. The second is the data to be stored in that cookie. The last parameter contains a value representing the number of days that you want the cookie to be available. This is then translated into a date that is stored in the cookie as an expiration date. The name, data, and expiration date are then written to the cookie using the document.cookie method.
The second function, GetCookie, accepts only one parameter. This parameter is the name of the cookie that you wish to retrieve. This function parses the data out from the cookie and returns it to the caller.
The last function, DeleteCookie, accepts a cookie name as a para-meter. It then deletes the cookie specified in the parameter. It does this by setting the expiration date to two days before the current date. Doing this causes the cookie to expire, removing it from your system.
Multiple Cookies and Multiple Elements
You could call this string a pseudo-array. This is because to process the elements in this string, the data is parsed out using the delimiter (shown here as the ` symbol) and placed into an array where the program can deal with it in a simpler fashion. After the array elements are manipulated, they are placed back into a delimiter string and written back to the cookie.
Another way to store multiple sets of information is by using more than one cookie. In your shopping basket application, you will, no doubt, want to store quantity information as well as item information. The quantity information can be stored in a position within the structure that corresponds to the correct item number. In other words, item element one and quantity element one relate to each other. Item element two and quantity element two are related, and so on.
As shown in Figure 2, the CookieBasket.html sample displays a list of items on the browser. When a user clicks on an item, a message is displayed in a dialog box telling the user the item that they selected has been added to their shopping basket. This item is then placed into the shopping cart. This is done first by retrieving the item cookie and quantity cookie data. This data is then placed into the corresponding arrays: itemArray and qtyArray. The item selected is then checked against existing items in the item array. If the item already exists in the array, the corresponding quantity element is incremented. If the selected item does not exist in the item array, a new element is written to the array and a quantity of one is added as a new element to the quantity array. These two arrays are then placed back into a delimited string of data and written to the clients machine as cookies.
The CookieBasket.html sample also contains two buttons. The first button, Clear Basket, will empty the shopping basket of all its items. This is done using the DeleteAllCookies function. One thing to notice is that this function calls the DeleteCookie function twice, specifying each cookie once. As shown in Figure 3, the second button, View Basket, will display a table listing the items and quantities currently in the shopping basket in a separate window.
One Tough Cookie
Internet Standards Track Protocol: info.internet.isi.edu/in-notes/rfc/files/rfc2109.txt
Netscapes cookie documentation: www.netscape.com/newsref/std/ cookie_spec.html
U.S. Department of Energy Computer Incident Advisory Capability Information Bulletin I-034: www.ciac.org/ciac/bulletins/i-034.shtml
Figure 2: Heres a view of the CookieBasket.html Web page.