In this section, we present how to use PHP sessions, showing how sessions are started and ended and how session variables are used. We list the PHP functions for building session-based web applications. Because not all browsers support cookies, and some users actively disable them, we describe how to use PHP sessions without relying on cookies. Finally, we show how to configure PHP session management with a discussion on the garbage collection used to remove old sessions and other configuration parameters.
An overview of PHP session management is shown in Figure 8-1. When a user first enters the session-based application by making a request to a page that starts a session, PHP generates a session ID and creates a file that stores the session-related variables. PHP sets a cookie to hold the session ID in the response the script generates. The browser then records the cookie and includes it in subsequent requests. In the example shown in Figure 8-1, the script welcome.php records session variables in the session store, and a request to next.php then has access to those variables because of the session ID.
Figure 8-1. The interaction between the browser and the server when initial requests are made to a session-based application
The out-of-the-box configuration of PHP session management uses disk-based files to store session variables. Using files as the session store is adequate for most applications in which the numbers of concurrent sessions are limited. A more scalable solution that uses a MySQL database as a session store is provided in Appendix D.
Starting a Session
PHP provides a
session_start( ) function that creates a new session and subsequently identifies and establishes an existing one. Either way, a call to the
) function initializes a session.
The first time a PHP script calls
session_start( ), a session identifier is generated, and, by default, a
Set-Cookie header field is included in the response. The response sets up a session cookie in the browser with the name
PHPSESSID and the value of the session identifier. The PHP session management automatically includes the cookie without the need to call to the
setcookie( ) or
header( ) functions.
The session identifier (ID) is a random string of 32 hexadecimal digits, such as
fcc17f071bca9bf7f85ca281094390b4. As with other cookies, the value of the session ID is made available to PHP scripts in the
$HTTP_COOKIE_VARS associative array and in the
When a new session is started, PHP creates a session file. With the default configuration, session files are written in the/tmp directory using the session identifier, prefixed with sess_, for the filename. The filename associated with our example session ID is /tmp/sess_fcc17f071bca9bf7f85ca281094390b4.
If a call is made to
session_start( ), and the request contains the
PHPSESSID cookie, PHP attempts to find the session file and initialize the associated session variables as discussed in the next section. However, if the identified session file can't be found,
session_start( ) creates an empty session file.