Example D-6. The sessionClose handler
// This function is executed on shutdown of the session. // Always returns true. function sessionClose($sess_id) { return true; }
sessionDestroy
When session_destroy( )
is called, the sessionDestroy( )
handler shown in Example D-7 is called. This function deletes the row identified by the $sess_id
argument from the table that holds the session variables.
Example D-7. The sessionDestroy handler
// This is called whenever the session_destroy( ) // function call is made. Returns true if the session // has successfully been deleted. function sessionDestroy($sess_id) { global $connection; global $session_table; $delete_query = "DELETE FROM $session_table WHERE session_id = '$sess_id'"; if (!($result = @ mysql_query($delete_query, $connection))) showerror( ); return true; }
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; }