I'm using the following class to handle sessions:
class DBHandler implements SessionHandlerInterface {
public function open($save_path, $name) {
try {
DBCxn::get();
return true;
} catch(PDOException $e) {
return false;
}
}
public function close() {
return true;
}
public function destroy($session_id) {
$sth = DBCxn::get()->prepare("DELETE FROM sessions WHERE session_id = ?");
$sth->execute(array($session_id));
return true;
}
public function gc($maxlifetime) {
$sth = DBCxn::get()->prepare("DELETE FROM sessions WHERE session_id = ?");
$sth->execute(array(time() . $maxlifetime));
return true;
}
public function read($session_id) {
$sth = DBCxn::get()->prepare("SELECT session_data FROM sessions WHERE session_id = ?");
$sth->execute(array($session_id));
$rows = $sth->fetchALL(PDO::FETCH_NUM);
if (count($rows) == 0) {
return '';
}
else {
return $rows[0][0];
}
}
public function write($session_id, $session_data) {
$sth = DBCxn::get()->prepare("UPDATE sessions SET session_data = ?, last_update = NOW() WHERE session_id = ?");
$sth->execute(array($session_data, $session_id));
if ($sth->rowCount() == 0) {
$sth = DBCxn::get()->prepare("INSERT INTO sessions (session_id, session_data, last_update) VALUES (?, ?, NOW())");
$sth->execute(array($session_id, $session_data));
}
}
}
session_set_save_handler(new DBHandler);
I then start the session, and then use a switch (that is accessed either through the get variable or ajax) to include content for the main div in index.php.
The switch has the following validation in case it is accessed via ajax:
if(!isset($_SESSION))
{
session_start();
}
if(isset($_SESSION['l'])) {
At the end of index.php the session is closed with session_write_close();
The DB table has a constraint for unique session ids.
For some reason, and only some times I'm getting the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '312505097fb815255103447952d0af61' for key 'PRIMARY'' in file_path:99 Stack trace: #0 file_path(99): PDOStatement->execute(Array) #1 'login|i:1;') #2 file_path(185): session_write_close() #3 {main} thrown in file_path:99
file_path:99 is the folowing line in the previously mentioned class "$sth->execute(array($session_id, $session_data));"
file_path:185 is session_write_close() at the end of index.php
What could be causing this error?
The write function is generating the duplication error. If the session data is same as store session data in DB then it will always return value 0 for $sth->rowCount(). Due to which it try to insert session data with same session_id and generate duplication error.