<?
/*
This is a PHP class definition for php session variable tracking
using cookies and a MySQL database table. Uses the mysql_class I
created for the database connections.
Last Modified on 8/27/2002 8:10AM Justin Koivisto [Koivi Media]
The following is the mysql syntax for creating the table this class will need.
CREATE TABLE session(
sessionid varchar(255) NOT NULL,
value blob NOT NULL,
changed timestamp(14),
PRIMARY KEY(sessionid)
);
*/
$SESSION_CLASS_INC=true;
if(!$MYSQL_CLASS_INC) require "_class.mysql.php";
class session_class extends mysql_class{
function session_class($db='',$user='phpuser',$pass='php',$host='localhost'){
// Turn off caching:
header("Expires: Sun, 12 Sep 1976 12:15:00 CST");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
$this->mysql_class($user,$pass,$host);
$this->SelectDB($db);
$this->s_id=$GLOBALS["my_id"]; // Comes from cookie value
$query="select value from session where sessionid='$this->s_id'";
if($this->Exists($query)) $this->good_session=TRUE;
}
// Start new session or load previous values (call before any output)
function Initiate(){
if($this->good_session){ // Means that the session is still alive
$query="select value from session where sessionid='$this->s_id'";
$q_result=$this->QueryItem($query);
$tmp=unserialize($q_result);
// While a new key-value pair exists. Assign the
// variable name $key equal to $value
while(list($key,$value)=each($tmp)){
// Set all session data so we can use it in the scripts.
$GLOBALS[$key]=$value;
$this->session_data[$key]=$value;
}
}else{ // Set a cookie to let us know about started the session
srand((double)microtime()*1000000);
$this->s_id=md5(uniqid(rand()));
setcookie("my_id",$this->s_id);
}
}
// Registers session variables from GLOBAL array
// You must use this function to update a session variable value
function Register($var){
$this->session_data[$var]=$GLOBALS[$var];
}
// Checks to see if a variable was registered - returns 1 if true
function IsRegistered($var){
if(isset($this->session_data[$var]))
return 1;
return 0;
}
// Unregisters a session variable
// Use this to remove a variable's value from the session data and GLOBALS array
function Unregister($var){
$this->session_data[$var]='';
$GLOBALS[$var]='';
}
// Stops a current session (removes data from db also)
function Destroy(){
$query="delete from session where sessionid='$this->s_id'";
@mysql_query($query,$this->id); // Don't want to see errors or warnings
unset($this->s_id);
unset($this->session_data);
unset($my_session_id);
}
// Saves the session array
function Save(){
// First let's get rid of that first '0' for the associative array
// element if it exists
reset($this->session_data);$var=key($this->session_data);
if(!isset($var)){ // If it was there, skip that element
next($this->session_data);
while($var=key($this->session_data)){
$tmp[$var]=$GLOBALS[$var];
next($this->session_data);
}
}else{ // It's not there, proceed as usual
for(reset($this->session_data);$var=key($this->session_data);next($this->session_data))
$tmp[$var]=$GLOBALS[$var];
}
$tmp=serialize($tmp);
$query="select changed from session where sessionid='$this->s_id'";
if($this->Exists($query)){
$query="update session set value='$tmp' where sessionid='$this->s_id'";
$this->Update($query);
}else{
// Insert the data if not there to update
$sqldate=date("YmdHis",time());
$query="insert into session(sessionid,value,changed) values('$this->s_id','$tmp',$sqldate)";
$this->Insert($query);
}
}
// Deletes sessions older argument's value in seconds
function RemoveExpired($seconds){
$sqldate=date("YmdHis", time() -($seconds * 60));
$query="delete from session where changed < '$sqldate'";
@mysql_query($query,$this->id); // Don't want to see errors or warnings
setcookie("my_id"); // delete old cookie
unset($GLOBALS[my_id]);
}
// Used to check the session id of the current session. Mainly for debugging,
// but may be useful for some applications.
function GetID(){
return $this->s_id;
}
}
?>