[Session] cleanup of the PDO storage
This commit is contained in:
parent
62594291e4
commit
51b753a6b8
@ -20,23 +20,24 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
|||||||
class PdoSessionHandler implements \SessionHandlerInterface
|
class PdoSessionHandler implements \SessionHandlerInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* PDO instance.
|
* @var \PDO PDO instance.
|
||||||
*
|
|
||||||
* @var \PDO
|
|
||||||
*/
|
*/
|
||||||
private $pdo;
|
private $pdo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database options.
|
* @var array Database options.
|
||||||
*
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
*/
|
||||||
private $dbOptions;
|
private $dbOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
|
* List of available options:
|
||||||
|
* * db_table: The name of the table [required]
|
||||||
|
* * db_id_col: The column where to store the session id [default: sess_id]
|
||||||
|
* * db_data_col: The column where to store the session data [default: sess_data]
|
||||||
|
* * db_time_col: The column where to store the timestamp [default: sess_time]
|
||||||
|
*
|
||||||
* @param \PDO $pdo A \PDO instance
|
* @param \PDO $pdo A \PDO instance
|
||||||
* @param array $dbOptions An associative array of DB options
|
* @param array $dbOptions An associative array of DB options
|
||||||
*
|
*
|
||||||
@ -166,6 +167,7 @@ class PdoSessionHandler implements \SessionHandlerInterface
|
|||||||
//session data can contain non binary safe characters so we need to encode it
|
//session data can contain non binary safe characters so we need to encode it
|
||||||
$encoded = base64_encode($data);
|
$encoded = base64_encode($data);
|
||||||
|
|
||||||
|
try {
|
||||||
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
|
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
|
||||||
// MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
|
// MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
|
||||||
// it could result in calling createNewSession() whereas the session already exists in
|
// it could result in calling createNewSession() whereas the session already exists in
|
||||||
@ -179,7 +181,6 @@ class PdoSessionHandler implements \SessionHandlerInterface
|
|||||||
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
|
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
} else {
|
} else {
|
||||||
try {
|
|
||||||
$stmt = $this->pdo->prepare("UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id");
|
$stmt = $this->pdo->prepare("UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id");
|
||||||
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
|
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
|
||||||
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
|
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
|
||||||
@ -191,10 +192,10 @@ class PdoSessionHandler implements \SessionHandlerInterface
|
|||||||
// session_regenerate_id()
|
// session_regenerate_id()
|
||||||
$this->createNewSession($id, $data);
|
$this->createNewSession($id, $data);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
|
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user