Did you look through the user comments for session_set_save_handler?
for reference, here's an OOP savehandler I wrote that implements the Zend save handler. feel free to use whatever you can from it... it uses
adodblite
Code: Select all
<?php
class MC2_Session_Adodblite implements Zend_Session_SaveHandler_Interface
{
/**
* Contains the data access object
*/
private $_db = null;
/**
* Contains user id
* @var integer
*/
private $_userId = -1;
/**
* Name of sessions table
*/
private $_sessionTable = 'sessions';
/**
* User's IP address
*/
private $_remoteAddr = null;
public function __construct($db, $userId = null, $sessionTable = null)
{
$this->_db = $db;
$this->_remoteAddr = $_SERVER['REMOTE_ADDR'];
if (!is_null($userId))
{
$this->setUserId($userId);
}
if (!is_null($sessionTable))
{
$this->_sessionTable = $sessionTable;
}
}
public function open($savepath, $name)
{
return true;
}
public function close()
{
/**
* No need to close my db connection
* since it's shared with my application
*/
return true;
}
public function read($id)
{
$query = "
SELECT " . $this->_sessionTable . ".data
FROM " . $this->_sessionTable . "
WHERE " . $this->_sessionTable . ".id = ?
";
if ($result = $this->_db->execute($query, array($id)))
{
return $result->fields['data'];
}
return null;
}
/**
* This method writes the current session data to the database
*
* @param $id integer
* @param $data string (serialized session data)
*/
public function write($id, $data)
{
$deleteQuery = "
DELETE FROM " . $this->_sessionTable . "
WHERE " . $this->_sessionTable . ".id = ?
";
$this->_db->execute($deleteQuery, array($id));
$insertQuery = "
INSERT INTO " . $this->_sessionTable . "
(id, user_id, user_ip, data, access)
VALUES (?, ?, ?, ?, ?)
";
$values = array(
$id,
$this->_userId,
$this->_remoteAddr,
$data,
time()
);
$result = $this->_db->execute($insertQuery, $values);
return ($result !== false);
}
public function destroy($id)
{
$deleteQuery = "
DELETE FROM " . $this->_sessionTable . "
WHERE " . $this->_sessionTable . ".id = ?
";
$this->_db->execute($deleteQuery, array($id));
}
public function gc($maxlifetime)
{
$old = time() - $maxlifetime;
$deleteQuery = "
DELETE FROM " . $this->_sessionTable . "
WHERE " . $this->_sessionTable . ".access < ?
";
$this->_db->execute($deleteQuery, array($old));
}
public function setUserId($id)
{
if (!ctype_digit($id))
{
throw new Exception('Invalid parameter passed to ' . __METHOD__ . ': expecting integer');
}
$this->_userId = $id;
}
}
?>
Play around with it a bit first, and then ask questions as they come up. It implements
Zend_Session_SaveHandler_Interface, but it could just as easily be called like this without implementing that by doing something like:
Code: Select all
function __construct() {
session_set_save_handler(array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc'));
register_shutdown_function('session_write_close');
session_start();
}
EDIT: OH! I almost forgot my db table schema:
Code: Select all
CREATE TABLE `sessions` (
`id` varchar(40) NOT NULL,
`user_id` mediumint(9) default NULL,
`user_ip` varchar(15) default NULL,
`data` text,
`access` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;