Database Reference
In-Depth Information
which is easily done by deleting session records having a timestamp older than the
current time more than the permitted lifetime:
function
mysql_sess_gc
(
$sess_maxlife
)
{
global
$mysql_sess_dbh
;
try
{
$stmt
=
"DELETE FROM php_session
WHERE update_time < NOW() - INTERVAL ? SECOND"
;
$sth
=
$mysql_sess_dbh
->
prepare
(
$stmt
);
$sth
->
execute
(
array
(
$sess_maxlife
));
}
catch
(
PDOException
$e
) {
/* do nothing */
}
return
(
TRUE
);
# ignore errors
}
Using the storage module.
Install the
Cookbook_Session.php
file in a public library direcā
tory accessible to your scripts. On my system, I put PHP library files in
/usr/
local/lib/mcb
and modify
php.ini
so that the
include_path
variable names that directory
(see
Recipe 2.3
). To try the storage module, install the following example script,
sess_track.php
, in your web tree and invoke it a few times to see how the information
display changes:
<?
php
# sess_track.php: session request counting/timestamping demonstration
require_once
"Cookbook_Session.php"
;
require_once
"Cookbook_Webutils.php"
;
# for make_ordered_list()
$title
=
"PHP Session Tracker"
;
# Open session and extract session values
session_start
();
$count
=
$_SESSION
[
"count"
];
$timestamp
=
$_SESSION
[
"timestamp"
];
# If the session is new, initialize the variables
if
(
!
isset
(
$count
))
$count
=
0
;
if
(
!
isset
(
$timestamp
))
$timestamp
=
array
();
# Increment counter, add current timestamp to timestamp array
++
$count
;
$timestamp
[]
=
date
(
"Y-m-d H:i:s T"
);