Chapter 24. Working with Cookies and Sessions

This chapter might as well have been called "Keeping Track," because its theme throughout is the problem of tracking interactions with users over longer periods of time than it takes to generate a single web page. We explain the extent of PHP support for extended user sessions and for setting and checking cookies, and then cover a couple of related techniques involving directly sending HTTP headers.

Sessions and cookies are closely allied concepts in PHP and in web programming more generally, largely because the best way to actually implement sessions is by using cookies. Sessions are a higher-level concept than cookies, and for this chapter we plan to start at the top and work our way down.

What's a Session?

What do we mean by a session? Informally, a session of web browsing is a period of time during which a particular person, while sitting at a particular machine, views a number of different web pages in his or her browser program and then calls it quits, either for the night or because the person in question actually has a life. If you run a web site that this person visits during that time, for your purposes the session runs from that person's first download of a page from your site through the last. For example, a Caribbean hotel's web site might enjoy a session of five pages duration in the middle of a real user's ...

Get PHP6 and MySQL® 6 Bible 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.