Here is what I have so far:
PHP Code:
<?php
class SessionManager {
/**
* When True Error Reporting Is Enabled
*
* @public bool
*/
public $ErrorReporting = false;
/**
* Session Life Time
*
* @public bool
*/
public $LifeTime;
/**
* Close
*/
function Close() {
return true;
}
/**
* Destroy
*
* @param int
*/
function Destroy($ID) {
$NewID = mysql_real_escape_string($ID);
$SessionData = $DB->Query("DELETE FROM sessions WHERE id = '$NewID'");
return true;
}
/**
* Garbage Collection
*/
function GarbageCollection() {
$SessionData = $DB->Query("DELETE FROM sessions WHERE expires < time()");
return true;
}
/**
* Session Manager
*/
function Manager() {
$this->LifeTime = get_cfg_var("session.gc_maxlifetime");
session_set_save_handler(
array(&$this, "Open"),
array(&$this, "Close"),
array(&$this, "Read"),
array(&$this, "Write"),
array(&$this, "Destroy"),
array(&$this, "GarbageCollection")
);
}
/**
* Open
*
* @params string, string
*/
function Open($SavePath, $SessionName) {
global $sess_save_path;
$sess_save_path = $SavePath;
return true;
}
/**
* Read
*
* @param int
*/
function Read($ID) {
$Data = "";
$Time = time();
$NewID = mysql_real_escape_string($ID);
$SessionData = $DB->Query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");
$NumRows = $DB->NumRows($SessionData);
if ($NumRows > 0) {
$Row = $DB->FetchAssoc($SessionData);
$Data = $Row['data'];
}
return $Data;
}
/**
* Write
*
* @params int, string
*/
function Write($ID, $Data) {
$Time = time() + $this->LifeTime;
$NewID = mysql_real_escape_string($ID);
$NewData = mysql_real_escape_string($Data);
$ReplaceSession = $DB->Query("REPLACE sessions (id, data, expires) VALUES ($NewID, $NewData, $Time)");
return true;
}
}
?>
I basically rewrote what I saw in some tutorials but changed a few things here and there to come up with the above class. Something I would like to know is would it be very hard to write a function similar to mysql_real_escape_string b/c I want this class to work with multiple database types.
Bookmarks