I’ve got a strange problem that’s been bugging me for a few days and is starting to get a little annoying. When logging in it seems to log in fine but it’s not saving the session data itself to the session table. The session is there somewhere as when I do a dump of the $_SESSION array i can see the session data but as soon as I click on any link within the site it looses the session data.
The problem is definatly somewhere in there as I commented out the including of it and the requiring of it and stuck a session_start() right at the start the sessions worked fine. I don’t think it’s the update or insert quries as I swapped out the prepping placeholder for a known word and it inserted ok. I’ve tested it in isolation from the other scripts and it still shows the same behaviour, whatever the cause it’s probably stareing me right in the face.
<?php
class session implements SessionHandlerInterface {
public function __construct($db) {
$this->db = $db;
session_set_save_handler($this);
session_start();
}
public function open($path, $name) {
$this->db;
return true;
}
public function close() {
$this->db = null;
return true;
}
public function read($sess_id)
{
try
{
$sql = "SELECT sess_data FROM ue_user_session WHERE sess_id = :id";
$stmt = $this->db->prepare($sql);
$stmt->execute(array(':id'=>$sess_id));
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
error_log('Error reading the session data table in the session reading method.');
error_log(' Query with error: '.$sql);
error_log(' Reason given:'.$e->getMessage()."\
");
return '';
}
if (count($res) > 0)
{
return isset($res[0]['']) ? $res[0][''] : '';
}
else
{
return '';
}
}
public function write($sess_id, $data) {
try {
$sql = "SELECT sess_data FROM ue_user_session WHERE sess_id = :id";
$stmt = $this->db->prepare($sql);
$stmt->execute(array(':id'=>$sess_id));
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
error_log('Error reading the session data table in the session writing method.');
error_log(' Query with error: '.$sql);
error_log(' Reason given:'.$e->getMessage()."\
");
return false;
}
try {
if (count($res) > 0) {
$sql = "
UPDATE
ue_user_session
SET
last_activity = NOW()
, sess_data = :sess_data
WHERE
sess_id = :id
";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':sess_data', $data);
$stmt->bindParam(':id', $sess_id);
} else {
$sql ="
INSERT INTO
ue_user_session
(
sess_id
, user
, start
, last_activity
, expires
, sess_data
)
VALUES
(
:id
, 0
, NOW()
, NOW()
, DATE_ADD(NOW(), INTERVAL 30 MINUTE)
, :sess_data
)
";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $sess_id);
$stmt->bindParam(':sess_data', $data);
}
$res = $stmt->execute();
}
catch (PDOException $e) {
error_log('Error writing to the session data table.');
error_log('Query with error: '.$sql);
error_log('Reason given:'.$e->getMessage()."\
");
return false;
}
return true;
}
public function destroy($sess_id)
{
try
{
$sql = "DELETE FROM ue_user_session WHERE sess_id = :id";
$stmt = $this->db->prepare($sql);
$stmt->execute(array(':id'=>$sess_id));
}
catch (PDOException $e)
{
error_log('Error destroying the session.');
error_log('Query with error: '.$sql);
error_log('Reason given:'.$e->errorMessage()."\
");
return false;
}
return true;
}
public function gc($ttl)
{
$end = time() - $ttl;
try
{
$sql = "DELETE FROM ue_user_session WHERE last_activity <:end";
$stmt = $this->db->prepare($sql);
$stmt->execute(array(':id'=>$end));
}
catch (PDOException $e)
{
error_log('Error with the garbage collection method of the session class.');
error_log('Query with error: '.$sql);
error_log('Reason given:'.$e->getMessage());
return false;
}
return true;
}
public function __destruct()
{
session_write_close();
}
}
?>
CREATE TABLE IF NOT EXISTS `ue_user_session` (
`sess_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`user` int(11) NOT NULL,
`start` datetime NOT NULL,
`last_activity` datetime NOT NULL,
`expires` datetime NOT NULL,
`sess_data` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;