prepare("SELECT * FROM compte WHERE email = :email LIMIT 1"); $stmt->execute(['email' => $email]); $compte = $stmt->fetch(); if ($compte && password_verify($mot_de_passe, $compte['mot_de_passe'])) { if (empty($compte['email_valide'])) { $erreur = $translations['erreur_email_non_valide']; } else { session_regenerate_id(true); // ✅ anti session fixation // 2) Connexion OK → on initialise la session $_SESSION['compte_id'] = $compte['id']; $_SESSION['role'] = strtolower(trim($compte['role'] ?? '')); $_SESSION['reference_id'] = $compte['reference_id']; $_SESSION['email'] = $compte['email']; $_SESSION['last_activity'] = time(); // 3) On gère la redirection prioritaire vers la page demandée $next = $_POST['next'] ?? $_GET['next'] ?? ($_SESSION['redirect_to'] ?? null); $next = next_safe($next); if ($next) { unset($_SESSION['redirect_to']); header('Location: ' . $next); exit; } // 4) Sinon, fallback par rôle if ($_SESSION['role'] === 'client') { header('Location: /public/client/mes_rendez_vous.php');exit; } elseif ($_SESSION['role'] === 'admin') { header('Location: /admin/index.php');exit; } elseif ($_SESSION['role'] === 'secretaire') { header('Location: /public/cabinet/dashboard.php');exit; } else { header('Location: /public/praticien/agenda.php');exit; } } } else { $erreur = $translations['erreur_identifiants']; } } } ?>