Chapter 10. Preserving State With Query Strings, Cookies, and Sessions

Most of the PHP scripts you created in previous chapters are very much one-shot affairs. Each time they run, they start with a "clean slate" of variables and other data. This is because each request that a browser makes to a Web server is independent of any previous requests. When a Web server receives a request to run a PHP script, it loads the script into its memory, runs it, then removes all trace of it from memory.

However, most of the Web applications you use today have a need to store data between browser requests. For example, a shopping cart needs to remember which items you have added to your cart, and a forum application needs to remember your identity whenever you post a message in the forum.

In other words, there is a need to preserve the current state of a user's interaction with an application from one request to the next.

You've already looked at a simple example of storing state in the previous chapter, when you used hidden form fields to store previously entered form data across each step of a three-stage registration form. Although filling in the registration form involved three separate browser requests — and therefore three separate runs of the PHP script — the script was able to "remember" the state of the registration process by storing it in the forms themselves.

Although this approach works perfectly well for simple cases, it has a few disadvantages. For example, it's a slow way to store ...

Get Beginning PHP 5.3 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.