diff --git a/index.php b/index.php index faa3cb02..1bf52097 100644 --- a/index.php +++ b/index.php @@ -88,30 +88,23 @@ if ( /** * Persistent (stay logged in) */ -if (isset($_COOKIE[COOKIE_PERSISTENT]) && $database) { - $table_sessions_exists = $database->tableExists('sessions'); +if (isset($_COOKIE[COOKIE_PERSISTENT]) && $database && !$_SESSION['user']->isLoggedIn()) { + $sessions = $database + ->query( + 'SELECT * + FROM `sessions` + WHERE `session` = "' . $_COOKIE[COOKIE_PERSISTENT] . '";' + ) + ->fetchAll(); - if ($table_sessions_exists) { - $sessions = $database - ->query( - 'SELECT * - FROM `sessions` - WHERE `session` = "' . $_COOKIE[COOKIE_PERSISTENT] . '";' - ) - ->fetchAll(); + if (false !== $sessions) { + foreach ($sessions as $session) { + $expires = strtotime($session['expires']); - if (false !== $sessions) { - $_SESSION['user'] = new User(); + if (time() < $expires) { + $_SESSION['user'] = User::getFromID($session['user']); - foreach ($sessions as $session) { - /** 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; - } + break; } } }