try mine:
<?php
/**
* Session
*
* The Session class provides methods to store PHP session
* data in the database
*
* @author Joshua M. McNeese <jmcneese@hotmail.com>
* @version $Revision: 1.1.1.1 $
* @internal $Id: Session.class.php,v 1.1.1.1 2003/06/12 09:25:29 jmcneese Exp $
*/
class Session extends PEAR
{
// {{{ Variables
/**
* Id of the session
*
* @var integer
*/
var $id;
/**
* Database object
*
* @var object
*/
var $db;
/**
* Options array
*
* @var array
*/
var $options = array("entropy" => 20,
"ttl" => 1600,
"dbtable" => "sessions");
// }}}
// {{{ Constructor
/**
* Constructor
*
* @access public
* @param object $db Previously instantiated PEAR DB object
* @return void
*/
function Session($db)
{
$this->PEAR();
$this->db = $db;
session_set_save_handler(
array(& $this, '_open'),
array(& $this, '_close'),
array(& $this, '_read'),
array(& $this, '_write'),
array(& $this, '_destroy'),
array(& $this, '_gc')
);
}
// }}}
// {{{ _open()
/**
* Open
*
* Empty method for compatibility
*
* @access private
* @return void
*/
function _open()
{
}
// }}}
// {{{ _close()
/**
* Close
*
* Empty method for compatibility
*
* @access private
* @return void
*/
function _close()
{
}
// }}}
// {{{ _read()
/**
* Read a session
*
* @access private
* @param integer $id
* @return string
*/
function _read($id)
{
$res = $this->db->getOne("SELECT data FROM " . $this->options['dbtable'] . " WHERE id = " . $this->db->quote($id));
if (DB::isError($res)) {
$this->raiseError("Cannot load session data for id". $id);
}
return $res;
}
// }}}
// {{{ _destroy()
/**
* Destroy a session
*
* @access private
* @param integer $id
* @return void
*/
function _destroy($id)
{
$res= $this->db->query("DELETE FROM " . $this->options['dbtable'] . " WHERE id = " . $this->db->quote($id));
if(DB::isError($result)) {
$this->raiseError("Cannot delete session for id $id: ". $result->getMessage());
}
}
// }}}
// {{{ _write()
/**
* Write to a session
*
* @access private
* @param integer $id
* @param string $data
* @return void
*/
function _write($id,$data)
{
$count = $this->db->getOne("SELECT COUNT(*) FROM ". $this->options['dbtable'] ." WHERE id = ". $this->db->quote($id));
if($count == 0) {
$sql = "INSERT INTO ". $this->options['dbtable'] ." (id,data,date) VALUES (". $this->db->quote($id) .",". $this->db->quote($data) .",NOW())";
$res = $this->db->query($sql);
if (DB::isError($res)) {
$this->raiseError("Cannot create new session: ". $res->getMessage());
}
} else {
$sql = "UPDATE ". $this->options['dbtable'] ." SET data = ". $this->db->quote($data) .", date = NOW() WHERE id = ". $this->db->quote($id);
$res = $this->db->query($sql);
if (DB::isError($res)) {
$this->raiseError("Cannot update session $id: ". $res->getMessage());
}
}
return $res;
}
// }}}
// {{{ _gc()
/**
* Garbage collection
*
* @access private
* @return void
*/
function _gc()
{
$this->db->query("DELETE FROM ". $this->options['dbtable'] ." WHERE date < NOW() - ". $this->options['ttl']);
}
// }}}
// {{{ Destructor
/**
* Destructor
*
* @access private
* @return void
*/
function _Session()
{
if($this->options['entropy'] > rand(0,100)) {
$this->_gc();
}
}
// }}}
}
?>
with this db_table:
DROP TABLE IF EXISTS `sessions`;
CREATE TABLE `sessions` (
`id` varchar(32) NOT NULL default '',
`data` text NOT NULL,
`date` timestamp(14) NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM COMMENT='Session data';