Chapter 10. Remembering Users: Cookies and Sessions
A web server is a lot like a clerk at a busy deli full of pushy customers. The customers at the deli shout requests: âI want a half pound of corned beef!â and âGive me a pound of pastrami, sliced thin!â The clerk scurries around slicing and wrapping to satisfy the requests. Web clients electronically shout requests (âGive me /catalog/yak.php!â or âHereâs a form submission for you!â), and the server, with the PHP engineâs help, electronically scurries around constructing responses to satisfy the requests.
The deli clerk has an advantage that the web server doesnât, though: a memory. She naturally ties together all the requests that come from a particular customer. The PHP engine and the web server canât do that without some extra steps. Thatâs where cookies come in.
A cookie identifies a particular web client to the web server and PHP engine. Each time a web client makes a request, it sends the cookie along with the request. The engine reads the cookie and figures out that a particular request is coming from the same web client that made previous requests, which were accompanied by the same cookie.
If deli customers were faced with a memory-deprived clerk, theyâd have to adopt the same strategy. Their requests for service would look like this:
- âIâm customer 56 and I want a half pound of corned beef.â
- âIâm customer 29 and I want three knishes.â
- âIâm customer 56 and I want two pounds ...
Get Learning PHP now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.