Prev | Current Page 536 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

gc_maxlifetime");
// Set the session expiration date
$expiration = time() + $lifetime;
CHAPTER 18 ?–  SESSION HANDLERS 467
// Insert the session data into the database
$query = "INSERT INTO sessioninfo
VALUES('$SID', '$expiration', '$value')";
$result = mysql_query($query);
// If the query fails, the session already exists.
// Therefore, update the session instead.
if (! $result) {
$query = "UPDATE sessioninfo SET
expiration = '$expiration',
value = '$value' WHERE
SID = '$SID' AND expiration >". time();
$result = mysql_query($query);
}
} // end mysql_session_write()
/*
* mysql_session_destroy()
* Deletes all session information having input SID (only one row)
*/
function mysql_session_destroy($SID) {
// Delete all session information having a particular SID
$query = "DELETE FROM sessioninfo
WHERE SID = '$SID'";
$result = mysql_query($query);
} // end mysql_session_destroy()
468 CHAPTER 18 ?–  SESSION HANDLERS
/*
* mysql_session_garbage_collect()
* Deletes all sessions that have expired.
*/
function mysql_session_garbage_collect($lifetime) {
// Delete all sessions older than a specific age
$query = "DELETE FROM sessioninfo
WHERE sess_expiration < ".time() - $lifetime;
$result = mysql_query($query);
return mysql_affected_rows($result);
} // end mysql_session_garbage_collect()
?>
Once these functions are defined, they can be tied to PHP??™s handler logic with a
call to session_set_save_handler().


Pages:
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548