Database Reference
In-Depth Information
<? php
# Cookbook_Session.php: MySQL-based session storage module
require_once "Cookbook.php" ;
# Define the handler routines
function mysql_sess_open ( $save_path , $sess_name ) ...
function mysql_sess_close () ...
function mysql_sess_read ( $sess_id ) ...
function mysql_sess_write ( $sess_id , $sess_data ) ...
function mysql_sess_destroy ( $sess_id ) ...
function mysql_sess_gc ( $gc_maxlife ) ...
# Initialize the connection identifier, select user-defined
# session handling, and register the handler routines
$mysql_sess_dbh = NULL ;
ini_set ( "session.save_handler" , "user" );
session_set_save_handler (
"mysql_sess_open" ,
"mysql_sess_close" ,
"mysql_sess_read" ,
"mysql_sess_write" ,
"mysql_sess_destroy" ,
"mysql_sess_gc"
);
?>
The library file includes Cookbook.php so that it can access the connection routine for
opening a connection to the cookbook database. Then it defines the handler routines
(we'll get to the details of these functions shortly). Finally, it initializes the connection
identifier, tells PHP to get ready to use a user-defined session storage manager, and
registers the handler functions. Thus, a PHP script that wants to store sessions in MySQL
performs all the necessary setup simply by including the Cookbook_Session.php file:
require_once "Cookbook_Session.php" ;
The interface provided by the Cookbook_Session.php library file ex‐
poses a global database connection identifier variable
( $mysql_sess_conn ) and a set of handler routines named
mysql_sess_open() , mysql_sess_close() , and so forth. Scripts that
use the library should avoid using these global names for other pur‐
poses.
Now let's implement each handler routine.
Opening a session. PHP passes two arguments to this function: the save path and the
session name. The save path applies to file-based storage, and we don't need the session
Search WWH ::




Custom Search