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:
Request arrives from client, without cookies
Send response, including cookie(s)
Next request arrives. Since we already sent cookies, those will be sent back to us in later requests
Send next response, also with cookies (either changed or unchanged)
Figure 6-1. Cookies being ...
The best content for your career. Discover unlimited learning
on demand for around $1/day.