Database Reference
In-Depth Information
These statements form the basis of the routines that make up our MySQL-backed storage
module. The primary function of the module is to open and close MySQL connections
and issue the proper statements at the appropriate times.
Writing the storage management routines.
A user-defined session storage module is im‐
plemented as a set of handler routines. To register them with PHP's session manager,
call
session_set_save_handler()
, where each argument is a handler routine name
specified as a string:
session_set_save_handler
(
"mysql_sess_open"
,
# function to open a session
"mysql_sess_close"
,
# function to close a session
"mysql_sess_read"
,
# function to read session data
"mysql_sess_write"
,
# function to write session data
"mysql_sess_destroy"
,
# function to destroy a session
"mysql_sess_gc"
# function to garbage-collect old sessions
);
The order of the handler routines must be as shown, but you can name them as you
like. They need not necessarily be named
mysql_sess_open()
,
mysql_sess_close()
,
and so forth. Write the routines according to the following specifications:
mysql_sess_open ($save_path, $sess_name)
Performs any actions necessary to begin a session.
$save_path
is the name of the
location where sessions should be stored; this is useful for file storage only.
$sess_name
indicates the name of the session identifier (for example,
PHPSESSID
).
A MySQL-based storage manager can ignore both arguments. The function returns
TRUE
or
FALSE
to indicate whether the session was opened successfully.
mysql_sess_close ()
Closes the session, returning
TRUE
for success or
FALSE
for failure.
mysql_sess_read ($sess_id)
Retrieves the data associated with the session identifier and returns it as a string. If
there is no such session, the function returns an empty string. If an error occurs, it
returns
FALSE
.
mysql_sess_write ($sess_id, $sess_data)
Saves the data associated with the session identifier, returning
TRUE
for success or
FALSE
for failure. PHP itself takes care of serializing and unserializing the session
contents, so the read and write functions need deal only with serialized strings.
mysql_sess_destroy ($sess_id)
Destroys the session and any data associated with it, returning
TRUE
for success or
FALSE
for failure. For MySQL-based storage, destroying a session amounts to de‐
leting the row from the
php_session
table associated with the session ID.