Topic: Save session in database
I'm trying to save sessions in my database, but it isn't working. I followed this guide: http://shiflett.org/articles/storing-se … a-database.
I'm using a database object/class (similiar to punbb's one) to write to the database, but all objects are destroyed before write is called. Check the first warning block: http://nl.php.net/manual/nl/function.se … andler.php. I don't really know how to solve it.
This are my session functions:
/* Session functions
---------------------------------------------------------------------------------------------*/
function start_session($db_save=true,$save_path=false)
{
// Save session in specified path or database
if ($save_path !== false)
{
ini_set('session.save_path',$save_path);
}
else if ($db_save === true)
{
session_set_save_handler(
'_open',
'_close',
'_read',
'_write',
'_destroy',
'_clean'
);
}
// Start the session
session_start();
// Regenerate session id if $_SESSION['initiated'] isn't set
if (!isset($_SESSION['initiated']))
{
session_regenerate_id();
$_SESSION['initiated'] = true;
}
// Generate a token if there isn't one
if (!isset($_SESSION['token']))
$_SESSION['token'] = sha1(md5(uniqid(rand(),true).$_SERVER['HTTP_USER_AGENT']));
}
function _open()
{
return true;
}
function _close()
{
return true;
}
function _read($id)
{
global $db;
if ($result = $db->query('SELECT data FROM %prefix%sessions WHERE id=\''.$db->escape($id).'\''))
{
if ($db->num_rows($result))
{
$row = $db->fetch($result);
return $row['data'];
}
}
return '';
}
function _write($id,$data)
{
global $db;
return $db->query('REPLACE INTO %prefix%sessions VALUES (\''.$db->escape($id).'\',\''.$db->escape(time()).'\',\''.$db->escape($data).'\')');
}
function _destroy($id)
{
global $db;
return $db->query('DELETE FROM %prefix%sessions WHERE id=\''.$db->escape($id).'\'');
}
function _clean($max)
{
global $db;
$old = time() - $max;
return $db->query('DELETE FROM %prefix%sessions WHERE access < \''.$db->escape($old).'\'');
}
And I use the first function (start_session(true);) to start it.
Can someone help me to solve this?