Database Reference
In-Depth Information
mysql_sess_gc ($gc_maxlife)
Performs garbage collection to remove old sessions. This function is invoked on a
probabilistic basis. When PHP receives a request for a page that uses sessions, it
calls the garbage collector with a probability defined by the
session.gc_probabil
ity
configuration variable in
php.ini
. For example, if the probability value is 1 (that
is, 1%), PHP calls the collector approximately once every hundred requests. If the
value is 100, it calls the collector for every request—probably more processing
overhead than you'd want.
The argument to
gc()
is the maximum session lifetime in seconds. Sessions older
than that are considered subject to removal. The function returns
TRUE
for success
or
FALSE
for failure.
To register the handler routines, call
session_set_save_handler()
, which should be
done in conjunction with informing PHP that you'll be using a user-defined storage
module. The default storage management method is defined by the
session.save_han
dler
configuration variable. You can change the method globally by modifying the
php.ini
initialization file, or within individual scripts:
• To change the storage method globally, edit
php.ini
. The default configuration set‐
ting specifies the use of file-based session storage management:
session
.
save_handler
=
files
;
Modify this to indicate that sessions will be handled by a user-level mechanism:
session
.
save_handler
=
user
;
If you use PHP as an Apache module, restart Apache after modifying
php.ini
so that
PHP notices the changes.
If you change the storage method globally, every PHP script that uses sessions will
be expected to provide its own storage management routines. This may have un‐
intended side effects for other script writers if they are unaware of the change. For
example, other developers that use the web server may want to continue using file-
based sessions.
• As an alternative to making a global change, specify a different storage method by
calling
ini_set()
on a per-script basis:
ini_set
(
"session.save_handler"
,
"user"
);
ini_set()
is less intrusive than a global configuration change. The storage manager
we develop here uses
ini_set()
, to trigger database-backed session storage only
for those scripts that request it.
To make it easy to access an alternative session storage module, it's useful to create a
library file,
Cookbook_Session.php
. The only thing a script need do to use the library file
is include it prior to starting the session. The outline of the file looks like this: