From fb278bfdd684ff676ed716301745d03e1b3f97c2 Mon Sep 17 00:00:00 2001 From: stephan Date: Tue, 7 Jul 2026 10:57:03 -0400 Subject: [PATCH] Refactor session handling and redirect logic for login flow This commit enhances the session management and redirect behavior in the login process. It introduces a new function, `fxCompteRedirectAfterLoginConsume`, to streamline the handling of post-login redirects while preventing loops for login-related pages. Additionally, comments are updated for clarity regarding the session's role in protecting access to certain pages. These changes aim to improve user experience and maintainability of the authentication flow. --- compte.php | 14 ++++++------- php/inc_fx_compte.php | 47 ++++++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/compte.php b/compte.php index b0d0b21..065c429 100644 --- a/compte.php +++ b/compte.php @@ -98,16 +98,16 @@ if (!empty($_GET['a']) && $_GET['code'] != 'bib_assignment') { $strCode = $_GET['a']; } -// empêcher d'accéder à une page où il faut être loggé et qu'on ne l'est pas -if (($strCode != "compte_login" && $strCode != "compte_motdepasse" && $strCode != "compte_nouveau" && $strCode != "compte_reset" && !isset($_SESSION['com_id'])) || (($strCode == "compte_login" || $strCode == "compte_motdepasse" || $strCode == "compte_nouveau" || $strCode == "compte_reset") && isset($_SESSION['com_id']))) { - - - - /// MSIN-4339 Sauvegarder l'URL demandée +// Page protégée sans session : sauver la cible puis formulaire de login (MSIN-4339). +if ($strCode != 'compte_login' && $strCode != 'compte_motdepasse' && $strCode != 'compte_nouveau' && $strCode != 'compte_reset' && !isset($_SESSION['com_id'])) { $_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI']; - header('Location: ' . $vDomaine . '/' . $strPage); + exit; +} +// Déjà connecté sur login / mot de passe / inscription : accueil compte, sans polluer redirect_after_login. +if (($strCode == 'compte_login' || $strCode == 'compte_motdepasse' || $strCode == 'compte_nouveau' || $strCode == 'compte_reset') && isset($_SESSION['com_id'])) { + header('Location: ' . $vDomaine . '/' . $strPage); exit; } diff --git a/php/inc_fx_compte.php b/php/inc_fx_compte.php index 8a975e9..1bb6a90 100644 --- a/php/inc_fx_compte.php +++ b/php/inc_fx_compte.php @@ -1162,6 +1162,29 @@ function fxCourrielPerduCompte($strLangue) exit; } +/** + * URL post-login MSIN-4339 — ignore login/mot de passe pour éviter les rebonds en boucle. + */ +function fxCompteRedirectAfterLoginConsume() +{ + if (empty($_SESSION['redirect_after_login'])) { + return null; + } + + $strUrl = $_SESSION['redirect_after_login']; + unset($_SESSION['redirect_after_login']); + + $strPath = parse_url($strUrl, PHP_URL_PATH) ?: ''; + if ($strPath === '/compte' || $strPath === '/account') { + return null; + } + if (preg_match('#/(compte|account)/(login|motdepasse|nouveau|reset)(/|$)#i', $strPath)) { + return null; + } + + return $strUrl; +} + function fxLoginCompte($infoLogin, $strLangue) { global $objDatabase, $vDomaine; @@ -1238,17 +1261,13 @@ function fxLoginCompte($infoLogin, $strLangue) $arrRetour['state'] = 'true'; - //MSIN-4339 - if (!empty($_SESSION['redirect_after_login'])) { - $url = $_SESSION['redirect_after_login']; - - - unset($_SESSION['redirect_after_login']); - header("Location: " . $url); + // MSIN-4339 — page protégée demandée avant login (pas login/mot de passe). + $strAfterLogin = fxCompteRedirectAfterLoginConsume(); + if ($strAfterLogin !== null) { + header('Location: ' . $strAfterLogin); exit; } - } else { if ($strLangue == 'fr') $_SESSION['msg'] = 'e112'; @@ -1274,16 +1293,8 @@ function fxLoginCompte($infoLogin, $strLangue) else $strPage = 'account'; - if (($arrRetour['state'] ?? '') === 'true' && !empty($infoCompte['com_id'])) { - require_once dirname(__FILE__) . '/inc_fx_promoteur_hub.php'; - if (fxPromoteurHubShouldUse($infoCompte['com_id'])) { - header('Location: ' . fxPromoteurHubUrl($strLangue)); - exit; - } - } - - //header("Location: " . $vDomaine . '/' . $strPage); - header("Location: " . fxGetReferer()); + // Un seul atterrissage : /compte (hub v2 ou accueil selon compte.php). + header('Location: ' . $vDomaine . '/' . $strPage); } exit;