prepare("UPDATE users SET last_activity_at = NOW() WHERE id = ?"); $stmtAct->execute([$userId]); } catch (Throwable $e) {} echo json_encode([ 'success' => true, 'remaining' => $idleTimeout, 'timeout' => $idleTimeout, 'timestamp' => $now ], JSON_UNESCAPED_UNICODE); exit; } if ($action === 'status') { $lastActivity = (int)($_SESSION['last_activity'] ?? $now); $elapsed = $now - $lastActivity; $remaining = max(0, $idleTimeout - $elapsed); echo json_encode([ 'success' => true, 'remaining' => $remaining, 'timeout' => $idleTimeout, 'timestamp' => $now ], JSON_UNESCAPED_UNICODE); exit; } if ($action === 'logout') { try { $db = get_db_connection(); $stmt = $db->prepare("UPDATE users SET remember_token = NULL, remember_token_expires_at = NULL WHERE id = ?"); $stmt->execute([$userId]); fociz_log_audit($userId, 'LOGOUT_SESSION_GUARD', 'users', $userId, ['ip' => fociz_get_client_ip()], 'SUCCESS', $currentUser['email'] ?? null); } catch (Throwable $e) { error_log("Session guard logout error: " . $e->getMessage()); } clear_day_session_cookie(); $_SESSION = []; if (session_status() === PHP_SESSION_ACTIVE) { @session_regenerate_id(true); @session_destroy(); } echo json_encode(['success' => true, 'message' => 'Succesvol uitgelogd.'], JSON_UNESCAPED_UNICODE); exit; } http_response_code(400); echo json_encode(['success' => false, 'error' => 'Onbekende sessieactie.'], JSON_UNESCAPED_UNICODE); exit;