Prev | Current Page 535 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


CHAPTER 18 ?–  SESSION HANDLERS 465
Listing 18-2. The MySQL Session Storage Handler
/*
* mysql_session_open()
* Opens a persistent server connection and selects the database.
*/
function mysql_session_open($session_path, $session_name) {
mysql_pconnect("localhost", "sessionmanager", "secret")
or die("Can't connect to MySQL server! ");
mysql_select_db("sessions")
or die("Can't select MySQL sessions database");
} // end mysql_session_open()
/*
* mysql_session_close()
* Doesn't actually do anything since the server connection is
* persistent. Keep in mind that although this function
* doesn't do anything in my particular implementation, it
* must nonetheless be defined.
*/
function mysql_session_close() {
return 1;
} // end mysql_session_close()
/*
* mysql_session_select()
* Reads the session data from the database
*/
466 CHAPTER 18 ?–  SESSION HANDLERS
function mysql_session_select($SID) {
$query = "SELECT value FROM sessioninfo
WHERE SID = '$SID' AND
expiration > ". time();
$result = mysql_query($query);
if (mysql_num_rows($result)) {
$row=mysql_fetch_assoc($result);
$value = $row['value'];
return $value;
} else {
return "";
}
} // end mysql_session_select()
/*
* mysql_session_write()
* This function writes the session data to the database.
* If that SID already exists, then the existing data will be updated.
*/
function mysql_session_write($SID, $value) {
// Retrieve the maximum session lifetime
$lifetime = get_cfg_var("session.


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