
Armed with the customer ID, the server can look up the customer's record in a database and
use this information to build an appropriate Web page to display. Depending on the customer's
known gambling habits, this page might consist of a poker hand, a listing of today's horse
races, or a slot machine.
The second cookie came from
joes-store.com. The scenario here is that the client is wandering
around the store, looking for good things to buy. When she finds a bargain and clicks on it, the
server builds a cookie containing the number of items and the product code and sends it back
to the client. As the client continues to wander around the store, the cookie is returned on
every new page request. As more purchases accumulate, the server adds them to the cookie.
In the figure, the cart contains three items, the last of which is desired in duplicate. Finally,
when the client clicks on
PROCEED TO CHECKOUT, the cookie, now containing the full list of
purchases, is sent along with the request. In this way the server knows exactly what has been
purchased.
The third cookie is for a Web portal. When the customer clicks on a link to the portal, the
browser sends over the cookie. This tells the portal to build a page containing the stock prices
for Sun Microsystems and Oracle, and the New York Jets football results. Since a cookie can be
up to 4 KB, there is plenty of room for more detailed preferences concerning newspaper
headlines, local weather, special offers, etc.
Cookies can also be used for the server's own benefit. For example, suppose a server wants to
keep track of how many unique visitors it has had and how many pages each one looked at
before leaving the site. When the first request comes in, there will be no accompanying cookie,
so the server sends back a cookie containing
Counter = 1. Subsequent clicks on that site will
send the cookie back to the server. Each time the counter is incremented and sent back to the
client. By keeping track of the counters, the server can see how many people give up after
seeing the first page, how many look at two pages, and so on.
Cookies have also been misused. In theory, cookies are only supposed to go back to the
originating site, but hackers have exploited numerous bugs in the browsers to capture cookies
not intended for them. Since some e-commerce sites put credit card numbers in cookies, the
potential for abuse is clear.
A controversial use of cookies is to secretly collect information about users' Web browsing
habits. It works like this. An advertising agency, say, Sneaky Ads, contacts major Web sites
and places banner ads for its corporate clients' products on their pages, for which it pays the
site owners a fee. Instead of giving the site a GIF or JPEG file to place on each page, it gives
them a URL to add to each page. Each URL it hands out contains a unique number in the file
part, such as
http://www.sneaky.com/382674902342.gif
When a user first visits a page, P, containing such an ad, the browser fetches the HTML file.
Then the browser inspects the HTML file and sees the link to the image file at
www.sneaky.com, so it sends a request there for the image. A GIF file containing an ad is
returned, along with a cookie containing a unique user ID, 3627239101 in
Fig. 7-25. Sneaky
records the fact that the user with this ID visited page
P. This is easy to do since the file
requested (
382674902342.gif) is referenced only on page P. Of course, the actual ad may
appear on thousands of pages, but each time with a different file name. Sneaky probably
collects a couple of pennies from the product manufacturer each time it ships out the ad.
Later, when the user visits another Web page containing any of Sneaky's ads, after the
browser has fetched the HTML file from the server, it sees the link to, say,
http://www.sneaky.com/493654919923.gif and requests that file. Since it already has a cookie
from the domain
sneaky.com, the browser includes Sneaky's cookie containing the user ID.
Sneaky now knows a second page the user has visited.