Previous Section  < Day Day Up >  Next Section

8.2 Activating Sessions

Sessions use a cookie called PHPSESSID. When you start a session on a page, the PHP interpreter checks for the presence of this cookie and sets it if it doesn't exist. The value of the PHPSESSID cookie is a random alphanumeric string. Each web client gets a different session ID. The session ID in the PHPSESSID cookie identifies that web client uniquely to the server. That lets the interpreter maintain separate piles of data for each web client.

The conversation between the web client and the server when starting up a session is illustrated in Figure 8-2.

Figure 8-2. Client and server communication when starting a session

To use a session in a page, call session_start( ) at the beginning of your script. Like setcookie( ), this function must be called before any output is sent. If you want to use sessions in all your pages, set the configuration directive session.auto_start to On. (Appendix A explains how to change configuration settings.) Once you do that, there's no need to call session_start( ) in each page.

    Previous Section  < Day Day Up >  Next Section