diff --git a/inc/check_login.php b/inc/check_login.php index 36d6428..60860bf 100755 --- a/inc/check_login.php +++ b/inc/check_login.php @@ -28,13 +28,36 @@ if (!isset($_SESSION['user_id']) && isset($_COOKIE['remember_token'])) { $update_stmt = mysqli_prepare($conn, $update_sql); mysqli_stmt_bind_param($update_stmt, "ss", $new_expires_at, $token); mysqli_stmt_execute($update_stmt); + mysqli_stmt_close($update_stmt); } else { // Token ist ungültig oder abgelaufen, Cookie löschen setcookie('remember_token', '', time() - 3600, "/"); } } -if (!isset($_SESSION['user_id'])) { +// 🔹 Letzte Aktivität aktualisieren (nur für eingeloggte Benutzer) +if (isset($_SESSION['user_id'])) { + $now = new DateTime(); + $last_update = $_SESSION['last_activity_update'] ?? null; + $update_needed = true; + + if ($last_update) { + $last = new DateTime($last_update); + if ($now->getTimestamp() - $last->getTimestamp() < 60) { + $update_needed = false; + } + } + + if ($update_needed) { + $stmt = mysqli_prepare($conn, "UPDATE users SET last_activity = NOW() WHERE id = ?"); + if ($stmt) { + mysqli_stmt_bind_param($stmt, "i", $_SESSION['user_id']); + mysqli_stmt_execute($stmt); + mysqli_stmt_close($stmt); + $_SESSION['last_activity_update'] = $now->format('Y-m-d H:i:s'); + } + } +} else { header('Location: login.php'); exit(); }