prepare("SELECT user_id FROM sessions WHERE id = ?"); $stmt->execute([$sessionId]); $result = $stmt->fetch(\PDO::FETCH_ASSOC); // If session not found in DB, it was deleted remotely if (!$result) { // Session was deleted - logout this user session_destroy(); session_start(); $_SESSION['error'] = 'Your session was terminated remotely. Please login again.'; header('Location: /login'); exit; } // If session exists but user_id doesn't match, something is wrong if ($result['user_id'] != $_SESSION['user_id']) { session_destroy(); session_start(); $_SESSION['error'] = 'Session validation failed. Please login again.'; header('Location: /login'); exit; } } catch (\Exception $e) { // If sessions table doesn't exist, allow normal operation (graceful fallback) error_log("Session validation failed: " . $e->getMessage()); } } }