Hello,
I'm in the process of switching to session_set_save_handler() and MySQL to handle sessions which is pretty cool but I'm having a problem trying to delete a single session.
If I create 3 sessions for example, the "data" field in the database would contain the session data like so:
sessTest|s:16:"AAAAAAAAAAAAAAAA";sessFoo|s:14:"BBBBBBBBBBBBBB";sessBar|s:14:"CCCCCCCCCCCCCC";
How can I delete just the session "sessFoo"??
I've tried unset($_SESSION['sessFoo']); but this deletes all three sessions. It appears to work when using sessions normally as files, but when the sessions are stored in the database it doesn't.
Below is the code I'm using to manage sessions with the database... Maybe the problem is in the "_write" function??
Thanks for any help!
Peter
<?php
function _open()
{
global $connMyConnection;
if (require_once($_SERVER['DOCUMENT_ROOT'].'/connMyConnection.php'))
{
return mysql_select_db($database_connMyConnection, $connMyConnection);
}
return FALSE;
}
function _close()
{
global $connMyConnection;
return mysql_close($connMyConnection);
}
function _read($id)
{
global $connMyConnection;
$id = mysql_real_escape_string($id);
$sql = "SELECT data
FROM sessions
WHERE id = '$id'";
if ($result = mysql_query($sql, $connMyConnection))
{
if (mysql_num_rows($result))
{
$record = mysql_fetch_assoc($result);
return $record['data'];
}
}
return '';
}
function _write($id, $data)
{
global $connMyConnection;
$access = time();
$id = mysql_real_escape_string($id);
$access = mysql_real_escape_string($access);
$expires = mysql_real_escape_string(300);
$data = mysql_real_escape_string($data);
$sql = "REPLACE
INTO sessions
VALUES ('$id', '$access', '$expires', '$data')";
return mysql_query($sql, $connMyConnection);
}
function _destroy($id)
{
global $connMyConnection;
$id = mysql_real_escape_string($id);
$sql = "DELETE
FROM sessions
WHERE id = '$id'";
return mysql_query($sql, $connMyConnection);
}
function _clean($max)
{
global $connMyConnection;
$old = time() - $max;
$old = mysql_real_escape_string($old);
$sql = "DELETE
FROM sessions
WHERE access < '$old'";
return mysql_query($sql, $connMyConnection);
}
?>
<?php
session_set_save_handler('_open', '_close', '_read', '_write', '_destroy', '_clean');
session_start();
$_SESSION['sessTest'] = "AAAAAAAAAAAAAAAA";
$_SESSION['sessFoo'] = "BBBBBBBBBBBBBB";
$_SESSION['sessBar'] = "CCCCCCCCCCCCCC";
?>