Cover by Lorna Jane Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Chapter 6. Cookies

The HTTP protocol is stateless. This means that we must include with every request all the information needed for the web server to serve the correct response. At least, in theory! In practice, that isn’t how we experience the web. As we browse around a shopping site, the website “remembers” which products we already viewed and which we placed in our basket - we experience our journeys on the web as a joined-up experience.

So how does this work? In fact we’re saving additional information and sending it with our web requests, by using cookies. Cookies are just key/value pairs; simple variables that can be stored on the client and then will be sent back to us on future requests. We might store a user’s choice of theme or accessibility settings, or even something as simple as dropping a cookie to know that they have visited the site before, or dismissed a particular alert message we showed them.

This isn’t the moment where I tell you how to bake cookies, although the instructions do read a little bit like a recipe. What happens when we work with cookies is something like this:

  1. Request arrives from client, without cookies
  2. Send response, including cookie(s)
  3. Next request arrives. Since we already sent cookies, those will be sent back to us in later requests
  4. Send next response, also with cookies (either changed or unchanged)
Cookies being exchanged in a series of requests
Figure 6-1. Cookies being ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required