Chapter 6: Tracking Visitors with Sessions
In This Chapter
Understanding sessions and cookies
Using sessions
This chapter looks at PHP’s built-in method for keeping track of visitors across multiple pages, called a session. The chapter starts out with an introduction to sessions and cookies and then jumps straight in by showing you how to use sessions to track visitors.
Understanding Sessions and Cookies
You’ve undoubtedly seen websites that track who you are, possibly welcoming you after you log in or presenting you with custom information about your account after logging in. There are a couple ways to do this, including sending the data along in a form with every request. But that isn’t secure and isn’t nearly flexible enough for today’s web applications. Luckily, there’s a better way — and it’s right at your fingertips: sessions.
Looking at sessions
A session in PHP is a secure way to track a user from page to page. With a session, you can store information about users, such as their e-mail address, name, phone number, and whatever other details you have, and automatically fill in that information wherever it’s needed on the site. For example, say that on login you load the user’s first name and e-mail address from your user database. You can store that information in a session, ...