diff --git a/index.php b/index.php index 72cf4edc..344fd29f 100644 --- a/index.php +++ b/index.php @@ -89,17 +89,21 @@ if ( /** * Persistent (stay logged in) */ -if (isset($_COOKIE[COOKIE_PERSISTENT])) { - $persistent = $database - ->query( - 'SELECT * - FROM `sessions` - WHERE `session` = "' . $_COOKIE[COOKIE_PERSISTENT] . '";' - ) - ->fetch(); +if (isset($_COOKIE[COOKIE_PERSISTENT]) && $database) { + $table_sessions_exists = $database->tableExists('sessions'); - if (false !== $persistent) { - $_SESSION['user'] = User::getFromID($persistent['user']); + if ($table_sessions_exists) { + $persistent = $database + ->query( + 'SELECT * + FROM `sessions` + WHERE `session` = "' . $_COOKIE[COOKIE_PERSISTENT] . '";' + ) + ->fetch(); + + if (false !== $persistent) { + $_SESSION['user'] = User::getFromID($persistent['user']); + } } } diff --git a/src/classes/database.php b/src/classes/database.php index 6ec76ba0..f3ff09a9 100644 --- a/src/classes/database.php +++ b/src/classes/database.php @@ -50,4 +50,25 @@ class Database { return $this->lastInsertId; } + + public function tableExists(string $table_to_check): bool + { + $tables = $this + ->query('SHOW TABLES;') + ->fetchAll(); + + if (!is_iterable($tables)) { + return false; + } + + foreach ($tables as $table_kv) { + $table = reset($table_kv); + + if ($table === $table_to_check) { + return true; + } + } + + return false; + } }