<?php
class class_sessions implements SessionHandlerInterface
{
/*
class_sessions
Damon Vaughn Caskey
2012_12_10
Override PHP's default session handling to store data in an MSSQL table.
*/
const c_iLife = 1440; //Default session time out (in seconds)
private $oDB = NULL; //Databse class object.
private $iLife = NULL; //Session time out.
function __construct($oDep, $iLife=self::c_iLife)
{
/*
Constructor
Damon Vaughn Caskey
2012_12_29
Class constructor.
*/
/* Set class vars. */
$this->iLife = $iLife; //Session time out.
/* Import object dependencies. */
$this->oDB = $oDep['DB'];
/* Verify object dependencies. */
if(!$this->oDB) trigger_error("Missing object dependency: Database.", E_USER_ERROR);
}
public function session_set($cID, $cValue=NULL)
{
/*
session_set
Damon Vaughn Caskey
2012_12_23
Wrapper to set value of a $_SESSION[] variable.
$cID: Session variable name/id.
$cValue: Value to set.
*/
$_SESSION[$cID] = $cValue;
}
public function session_get($cID)
{
/*
session_get
Damon Vaughn Caskey
2012_12_23
Wrapper to aquire value in a $_SESSION[] variable.
$cID: Session variable name/id.
*/
$cValue = NULL; //Value to return.
/* Get session value if any */
if(isset($_SESSION[$cID]))
{
$cValue = $_SESSION[$cID];
}
/* Return value. */
return $cValue;
}
public function open($savePath, $sessionName)
{
/*
open
Damon Vaughn Caskey
2012_12_10
Set database class object for other session functions. Called by PHP to open session.
$savePath: Path to locate session file. Unused.
$sessionName: Name of session file. Unused.
*/
/* Return TRUE. */
return true;
}
public function close()
{
/*
close
Damon Vaughn Caskey
2012_12_10
Filler; function is called by PHP to close session.
*/
/* Return TRUE. */
return true;
}
public function read($cID)
{
/*
read
Damon Vaughn Caskey
2012_12_10
Locate and read session data from database.
$cID = Session ID.
*/
$cData = NULL; //Final output.
$cQuery = NULL; //Query string.
$cTime = date(constants::c_cDateF); //Current time.
$cParams = NULL; //Parameter array.
/* Build query string. */
$cQuery = "SELECT session_data
FROM tbl_php_sessions
WHERE
session_id = ?
AND
expire > ?";
/* Apply parameters. */
$cParams = array(&$cID, &$cTime);
/* Execute query. */
$this->oDB->db_basic_select($cQuery, $cParams);
/* Get result and pass to local var(s). */
if($this->oDB->rDBResult)
{
/* Set line array. */
$this->oDB->db_line();
/* Get session data. */
$cData = $this->oDB->cDBLine['session_data'];
}
/* Return results. */
return $cData;
}
public function write($cID, $cData)
{
/*
write
Damon Vaughn Caskey
2012_12_10
Update or insert session data. Note that only ID, Expire, and Session Data are
required. Other data is to aid in debugging.
$cID = Session ID.
$cData = Session data.
*/
$cQuery = NULL; //Query string.
$cTime = NULL; //Current time.
$cLoc = $_SERVER["PHP_SELF"]; //Current file.
$cIP = $_SERVER['REMOTE_ADDR']; //Client IP address.
/* Calculate epirire time. */
$cTime = date(constants::c_cDateF, time()+$this->iLife);
/* Ensure IP string is <= 15. Anything over is a MAC or unexpected (and useless) value. */
$cIP = substr($cIP, 0, 15);
/* Build query string. */
$cQuery ="MERGE INTO tbl_php_sessions
USING
(SELECT ? AS Search_Col) AS SRC
ON
tbl_php_sessions.session_id = SRC.Search_Col
WHEN MATCHED THEN
UPDATE SET
session_data = ?,
expire = ?,
source = ?,
ip = ?
WHEN NOT MATCHED THEN
INSERT (session_id, session_data, expire, source, ip)
VALUES (SRC.Search_Col, ?, ?, ?, ?);";
/* Apply parameters. */
$cParams = array(&$cID,
&$cData,
&$cTime,
&$cLoc,
&$cIP,
&$cData,
&$cTime,
&$cLoc,
&$cIP);
/* Execute query. */
$this->oDB->db_basic_action($cQuery, $cParams);
/* Return TRUE. */
return true;
}
public function destroy($cID)
{
/*
destroy
Damon Vaughn Caskey
2012_12_10
Delete current session.
$cID: Session ID.
*/
$cQuery = NULL; //Query string.
$cParams = NULL; //Parameter array.
/* Build query string. */
$cQuery = "DELETE FROM tbl_php_sessions WHERE session_id = ?";
/* Apply parameters. */
$cParams = array(&$cID);
/* Execute query. */
$this->oDB->db_basic_action($cQuery, $cParams);
/* Return TRUE. */
return true;
}
public function gc($maxlifetime)
{
/*
gc (Garbage Cleanup)
Damon Vaughn Caskey
2012_12_10
Delete expired session data.
$maxlifetime: Expire time. Unused.
*/
$cTime = date(constants::c_cDateF); //Current time.
$cQuery = NULL; //Query string.
$cParams = NULL; //Parameter array.
/* Build query string. */
$cQuery = "DELETE FROM tbl_php_sessions WHERE expire < ?";
/* Apply parameters. */
$cParams = array(&$cTime);
/* Execute query. */
$this->oDB->db_basic_action($cQuery, $cParams);
/* Return TRUE. */
return true;
}
}
Like this:
Like Loading...