In the HTTP protocol (the P
in HTTP), the browser and the
server talk to each other to get/send web pages. A typical request
(from browser to server) looks like this:
GET /index.html HTTP/1.0 Accept: text/html, image/gif, image/jpeg Accept-Language: en
Typical reply (from server to browser):
HTTP/1.0 200 OK Date: Fri, 20 Apr 2005 16:20:00 GMT Server: Apache/1.3.9 (Linux) Content-Length: 141 Content-Type: text/html <html> <body> The web page stuff. </body> </html>
HTTP/1.0 200 OK
Content-Length: 141
Content-Type: text/html
Set-Cookie: user_id=12345; domain=.wellesley.edu;
expires=Mon, 23-Apr-2005
... content follows
GET /index.html HTTP/1.0 Accept: text/html, image/gif, image/jpeg Accept-Language: en Cookie: user_id=12345
The document returned by the server might have some JavaScript in it, and the JavaScript code can also look at the cookie and customize things. The JavaScript code can also set cookies.
Here are some things we might remember in a cooke:
idto identify repeat visitors
Let's look at some of these options. All of them will be based on the following JS functions:
Suppose we want to do something special for a first-time visitor,
but not for return visits. Maybe a special introduction or greeting,
as we did at the top of this section. How can we tell whether someone
has visited the page before? We could set a cookie. We might set a
cookie like first_time=no. Remember that cookies have
both a name and a value. Also, we have to consider that for a
first-time visitor, no cookie will be set, so we have to notice
the absence of the cookie.
The code is here:
At the top of this section is a personalized
greeting. (We
should make it less obnoxious by using forms instead
of prompt(), but the code is simpler this way.) The page
remembers the user's name for a full year, unless they choose to forget
it.
The basic idea is to set a cookie with the user's name. When the
page loads, see if there is a cookie set. If so, generate some
personalized text, including the forget me
option. If not,
generate some text to encourage the person to login.
Websites like Amazon.com, Facebook, and many others work like this, only instead of saving your name as a cookie, they also (or instead) save some unique identifier, which is usually a very long number, and they use the unique identifier to look up your name in a database.
The code is here:
Some details:
deletethe cookie by setting its expiration date to yesterday.
We can also save user preference settings as a cookie. For example, the top of this section has a variant of the accessibility bar we looked at earlier in the semester. This version saves the user's preferred font size as a cookie, and so the user doesn't have to re-choose the font-size each time he or she visits.
Here is a link that will report the current font size
Even better, because of the path
part of the cookie, we can
allow the cookie to be retrieved by other parts of our website, so
that the pages can use a common set of cookies, and
even communicate
with them. For example, the font size you
choose for this page also applies to this other
page.
The code is here:
We could improve this in several ways. For example, once you've chosen your font size, the accessibility bar could be hidden. If you needed it back, you chould just clear your cookies. Try it!
In all our examples, the server always returns the same page, whether there is a cookie or not. In general, the server might do different things depending on the cookie. For example, Amazon.com returns a special page that includes your wish-list and such. Facebook returns an entirely customized page.
From the W3C Security FAQ, some more information about cookies and their security risks.