prepare($sql); $stmt->bindParam(":username", $username, PDO::PARAM_STR); if ($stmt->execute()) { if ($stmt->rowCount() === 1) { $user = $stmt->fetch(PDO::FETCH_ASSOC); $hash = $user['password_hash']; $first = (int)$user['first_login']; // 1️⃣ Nur wenn wirklich First Login ODER kein Hash vorhanden if ($first === 1 || empty($hash)) { $_SESSION['set_pw_user'] = (int)$user['id']; header("location: set_password.php"); exit; } // 2️⃣ Passwort prüfen if (!password_verify($password, $hash)) { $password_err = "Falsches Passwort."; } else { session_regenerate_id(true); $_SESSION["loggedin"] = true; $_SESSION["id"] = (int)$user["id"]; $_SESSION["username"] = $user["username"]; $_SESSION["name"] = $user["name"]; $_SESSION["role"] = $user["role"]; // last_login setzen $upd = $pdo->prepare("UPDATE users SET last_login = :ts WHERE id = :id"); $upd->execute([ ':ts' => time(), ':id' => (int)$user['id'] ]); header("location: dashboard.php"); exit; } } else { $username_err = "Dieser Benutzer existiert nicht."; } } else { echo "Fehler. Bitte später erneut versuchen."; } } } ?>