Garbage collection
The last handler to be defined is the garbage collection function. Example D-8 shows the implementation of sessionGC( ), which queries for all session rows that have been dormant for $max_lifetime seconds. PHP passes the value set in the session.gc_maxlifetime parameter of the php.ini file. The time a session has been dormant is calculated by subtracting the last update time-held in the session row-from the current time.
Example D-8. Garbage collection handler
// This function is called on a session's start up with
// the probability specified in session.gc_probability.
// Performs garbage collection by removing all sessions
// that haven't been updated in the last $max_lifetime
// seconds as set in session.gc_maxlifetime.
// Returns true if the DELETE query succeeded.
function sessionGC($max_lifetime)
{
global $connection;
global $session_table;
$time_stamp = getMicroTime( );
$delete_query =
"DELETE FROM $session_table
WHERE last_accessed < ($time_stamp - $max_lifetime)";
if (!($result = @ mysql_query($delete_query,
$connection)))
showerror( );
return true;
}