Hi Guys,
I am currently trying to write a bit of code that handles the sessions in a mysql database.
Setting the Data - Worknig Fine
Reading the Data - Clears the session data
I am having problems with reading the data back again. When i try this it clears the data stored in teh session.
Here is the code for my Session Class:
<?php
/**
* File : session.lib..php
* Project : Sessions
* Author : AD
* Description : Sessions stored into the database
* Version : 1.0 - 27/07/2009 - Initial Creation
**/
class SessionManager
{
var $life_time;
function SessionManager()
{
// Read the maxlifetime setting from PHP
$this->life_time = get_cfg_var("session.gc_maxlifetime");
// Register this object as the session handler
session_set_save_handler(
array($this, "open"),
array($this, "close"),
array($this, "read"),
array($this, "write"),
array($this, "destroy"),
array($this, "gc")
);
$this->sql = new Site_Sql; // Initiates the sql class
}
function open($save_path, $session_name)
{
global $sess_save_path;
$sess_save_path = $save_path;
// Don't need to do anything. Just return TRUE.
return true;
}
function close()
{
return true;
}
function read( $id )
{
// Set empty result
$data = '';
// Fetch session data from the selected database
$time = time();
$this->sql->Connect();
$_query = sprintf("SELECT session_data FROM tbl_sessions WHERE session_id='%s' AND expires > '%s'", mysql_escape_string($id), mysql_escape_string($time));
//debug
echo $_query;
$results = $this->sql->Query($_query);
$row_count = $results->FetchNUM();
if($row_count > 0)
{
$row= $results->FetchRow();
$data = $row['session_data'];
}
$this->sql->Close();
return $data;
}
function write( $id, $data )
{
// Build query
$time = time() + $this->life_time;
$this->sql->Connect();
$_query = sprintf("INSERT INTO tbl_sessions (session_id, session_data, expires) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE session_data='%s', expires='%s'", mysql_escape_string($id), mysql_escape_string($data), mysql_escape_string($time), mysql_escape_string($data), mysql_escape_string($time));
// Debug
echo $_query;
$results = $this->sql->Query($_query);
$this->sql->Close();
return TRUE;
}
function destroy( $id )
{
// Build query
$newid = mysql_real_escape_string($id);
$this->sql->Connect();
$_query = sprintf("DELETE FROM tbl_sessions WHERE session_id='%s'", mysql_escape_string($id));
// debug
echo $_query;
$results = $this->sql->Query($_query);
$this->sql->Close();
return TRUE;
}
function gc()
{
// Garbage Collection
$this->sql->Connect();
$_query = sprintf("DELETE FROM tbl_sessions WHERE expires < UNIX_TIMESTAMP()");
//debug
echo $_query;
$results = $this->sql->Query($_query);
$this->sql->Close();
// Always return TRUE
return true;
}
}
?>
and here is the code to set the session:
<?php
echo "Sessions<br><br>";
echo "Creating Session: info...";
$SessionManager= new SessionManager;
session_start();
$_SESSION['info'] = "This is a variable in a session";
echo "...Done<br><br>";
echo "Click <a href=\"view.php\">here</a> to view the session<br><br>";
echo "Debug:<br><br>";
session_write_close();
?>
and the code for viewing the session:
<?php
echo "Sessions<br><br>";
$SessionManager= new SessionManager;
session_start();
echo "Session Name: info<br>";
echo "Session Data: " . $_SESSION['infooo'] . "<br><br>";
echo "Click <a href=\"del.php\">here</a> to delete the session<br><br>";
echo "Debug:<br><br>";
?>
I hope someone can help me with this as its driving me up the wall!
Thankyou in advance.
Tony