Improve persistent sessions

This commit is contained in:
grandeljay 2022-11-29 12:02:15 +01:00
parent 97480fd46f
commit 9e80ce02b1

View file

@ -88,30 +88,23 @@ if (
/** /**
* Persistent (stay logged in) * Persistent (stay logged in)
*/ */
if (isset($_COOKIE[COOKIE_PERSISTENT]) && $database) { if (isset($_COOKIE[COOKIE_PERSISTENT]) && $database && !$_SESSION['user']->isLoggedIn()) {
$table_sessions_exists = $database->tableExists('sessions'); $sessions = $database
->query(
'SELECT *
FROM `sessions`
WHERE `session` = "' . $_COOKIE[COOKIE_PERSISTENT] . '";'
)
->fetchAll();
if ($table_sessions_exists) { if (false !== $sessions) {
$sessions = $database foreach ($sessions as $session) {
->query( $expires = strtotime($session['expires']);
'SELECT *
FROM `sessions`
WHERE `session` = "' . $_COOKIE[COOKIE_PERSISTENT] . '";'
)
->fetchAll();
if (false !== $sessions) { if (time() < $expires) {
$_SESSION['user'] = new User(); $_SESSION['user'] = User::getFromID($session['user']);
foreach ($sessions as $session) { break;
/** Column sessions.expires was added in v0.7.1. */
$expires = strtotime($session['expires'] ?? date('Y-m-d H:i:s', time() + 1));
if (time() < $expires) {
$_SESSION['user'] = User::getFromID($session['user']);
break;
}
} }
} }
} }