check session validity by DatabaseSessionHandler

This commit is contained in:
Bence Pőcze 2023-05-02 12:21:05 +02:00
parent 0da1d00c3d
commit 824ee0cd1f
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -13,14 +13,17 @@ class DatabaseSessionHandler implements ISessionHandler
private string $table;
private DateTime $shouldBeNewerThan;
private bool $exists = false;
private bool $written = false;
public function __construct(IConnection $dbConnection, string $table)
public function __construct(IConnection $dbConnection, string $table, DateTime $shouldBeNewerThan)
{
$this->dbConnection = $dbConnection;
$this->table = $table;
$this->shouldBeNewerThan = $shouldBeNewerThan;
}
public function open($savePath, $sessionName): bool
@ -36,14 +39,17 @@ class DatabaseSessionHandler implements ISessionHandler
public function read($id): string
{
$select = new Select($this->dbConnection, $this->table);
$select->columns(['data']);
$select->columns(['data', 'updated']);
$select->whereId(substr($id, 0, 32));
$result = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
if ($result === null) {
return '';
}
if (new DateTime($result['updated']) < $this->shouldBeNewerThan) {
$this->destroy($id);
return '';
}
$this->exists = true;