How to redirect to the previous page after a login

To make it fast i send mail with urls to my customers. But to reach the link you have to be connected so before reach an $_get[‘action’] you have to log in to my ConnexionController.

The main issue is, my ConnexionController after a log in redirect to the home page of my website and dont give a direct acces to the initial url the customer clicked for.

Here is a part of the code that make the connection.

if (!isset($_SESSION['UserID']) && htmlspecialchars($_GET['action']) != 'connectionIntranet' && htmlspecialchars($_GET['action']) != 'nouveauMotDePasse' && htmlspecialchars($_GET['action']) != 'recupeMotDePasse' && htmlspecialchars($_GET['action']) != 'recupeMotDePasseAttente')
    header("location: index.php?action=connectionIntranet");

if (isset($_POST['userName']) && isset($_POST['password'])) {
    $resConnect = $nrj_user->connect(htmlspecialchars($_POST['userName']), htmlspecialchars($_POST['password']));
    if ($resConnect > 0) { //connexion reussi
        $_SESSION['UserID'] = $resConnect;
        $infoUser = $nrj_user->getUserInfos($resConnect);
        if ($infoUser) { //Infos récupérés
            $_SESSION['UserID'] = $infoUser['UserID'];
            $_SESSION['IdentUser'] = $infoUser['IdentUser'];
            $_SESSION['initiales'] = $infoUser['initiales'];
            $_SESSION['Nom'] = $infoUser['Nom'];
            $_SESSION['Prenom'] = $infoUser['Prenom'];
            $_SESSION['TPortable'] = $infoUser['TPortable'];
            $_SESSION['email'] = $infoUser['email'];
            $_SESSION['teldom'] = $infoUser['teldom'];
            $_SESSION['TBureau'] = $infoUser['TBureau'];
            $_SESSION['civilite'] = $infoUser['civilite'];
            $_SESSION['Adress1'] = $infoUser['Adress1'];
            $_SESSION['Adress2'] = $infoUser['Adress2'];
            $_SESSION['CodPost'] = $infoUser['CodPost'];
            $_SESSION['Ville'] = $infoUser['Ville'];
            $_SESSION['TFax'] = $infoUser['TFax'];
            $_SESSION['entre_le'] = $infoUser['entre_le'];
            $_SESSION['sortie_le'] = $infoUser['sortie_le'];
            $_SESSION['modif_le'] = $infoUser['modif_le'];
            $_SESSION['modif_par'] = $infoUser['modif_par'];
            $_SESSION['Dpt_Id'] = $infoUser['Dpt_id'];
            $_SESSION['ref_cde'] = $infoUser['ref_cde'];
            $_SESSION['modif_pw'] = $infoUser['modif_pw'];

            header("location: index.php?action=AccueilIntranet");
        }
    } else
        $mesConnectionErreur = 'Identifiant ou mot de passe incorrecte';```

I already tried to save the url like this 

$action = $_SERVER[‘REQUEST_URI’];
$_POST[‘action’] = $action;

but i've got issues on the finals steps.

Thank you