Location: PHPKode > scripts > phpMysqlSessions > phpmysqlsessions/_class.session.php
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.

   sessionid varchar(255) NOT NULL,
   value blob NOT NULL,
   changed timestamp(14),
   PRIMARY KEY(sessionid)

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->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'";

			//  While a new key-value pair exists. Assign the
			//  variable name $key equal to $value
				//  Set all session data so we can use it in the scripts.
		}else{	//  Set a cookie to let us know about started the session

	//  Registers session variables from GLOBAL array
	//  You must use this function to update a session variable value
	function Register($var){

	//  Checks to see if a variable was registered - returns 1 if true
	function IsRegistered($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){

	//  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

	//  Saves the session array
	function Save(){
		//  First let's get rid of that first '0' for the associative array
		//  element if it exists
		if(!isset($var)){ //  If it was there, skip that element
		}else{	//  It's not there, proceed as usual
		$query="select changed from session where sessionid='$this->s_id'";
			$query="update session set value='$tmp' where sessionid='$this->s_id'";
			//  Insert the data if not there to update
			$query="insert into session(sessionid,value,changed) values('$this->s_id','$tmp',$sqldate)";

	//  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
	//  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;

Return current item: phpMysqlSessions