Database Reference
In-Depth Information
assumed not to be enabled here. ( Recipe 20.5 discusses the security implications of
register_globals , although it is no longer an issue as of PHP 5.4, when it was re‐
moved.)
The PHP session management interface
PHP's session management is based on a small number of functions, all of which are
documented in the PHP manual. The following list describes those we use:
session_start ()
Opens a session and extracts any variables previously stored in it, making them
available in the script's global namespace. For example, a session variable named x
becomes available as $_SESSION["x"] . This function must be called first before
using the relevant session variable array.
session_write_close ()
Writes the session data and closes the session. The PHP documentation indicates
that normally you need not call this function because PHP saves an open session
automatically when your script ends. However, it appears that in PHP 5, that might
not always be true when you provide your own session handler. To be safe, call this
function to save your changes.
session_destroy ()
Removes the session and any data associated with it.
Specifying a user-defined storage module
The PHP session management interface just described specifies nothing about backing
store or how session information actually gets saved. By default, PHP uses temporary
files to store session data, but the session interface is extensible to permit other storage
modules. To override the default storage method and store session data in MySQL, do
this:
1. Set up a table to hold session records, and write the routines that implement the
storage module. These actions are done once, prior to writing any scripts that use
the new module.
2. Tell PHP that you're supplying a user-defined storage manager. Do this globally in
php.ini (in which case you make the change once), or within individual scripts (in
which case it's necessary to declare your intent in each script).
3. Register the storage module routines within each script that uses the module.
Creating the session table. Any MySQL-based storage module needs a table in which to
store session information. Create a table named php_session defined as follows:
Search WWH ::




Custom Search