How to redirect the user to the page they were trying to access before login was requested?

Asked

Viewed 50 times

2

Imagine the following scenario:

I have a website with an application form for people who want to apply for a particular position.

What happens nowadays is that when the person submits the registration, receives an automatic email indicating that it was submitted for approval and is sent, also, another automatic email to the person responsible for managing these registrations. In this last email is presented an access link to the backoffice, which is behind all this, where it is possible to approve or reject this registration, triggering a set of actions.

It turns out that this page is private (only for members of the organization) and if the session is not created, a redirect is made to the login page.

My question is how, after logging in, I can redirect the person to the link I was trying to access earlier (the approval/rejection of that particular entry).

If possible, give me a practical example, please.

Thank you very much.

4 answers

2

Whenever redirecting to the login screen, you pass the current page as parameter,and when logging in you check if this parameter is filled in, if you are redirecting to the page that is saved in it.

  • Right. And how do you recommend passing this parameter to the login page, and what makes me redirect to login is the following code? <?php&#xA;&#xA;if (!isset($_SESSION)) session_start();&#xA; &#xA; // Verifica se não há a variável da sessão que identifica o usuário&#xA; if ($_SESSION['nivelAcesso'] != 1) {&#xA; // Destrói a sessão por segurança&#xA; session_destroy();&#xA; // Redireciona o visitante de volta pro login&#xA; header("Location: /"); &#xA; exit;&#xA; }&#xA;&#xA;require_once "config.php";&#xA; &#xA;// Verificar se está logado, se não reencaminha para a página de login&#xA;&#xA;?>

  • maybe if in this case you already redirected to the login page example : header("Location: /login.php? origin=/user/list");

  • uses $redirect = $_SERVER['REQUEST_URI']; on the LOGIN page itself to grab the previous page and then uses Header("Location: " . $redirect . "");

  • worked ......?

2

Before redirecting the user to login, create a session with the URL that the user tried to access.

After it logs in, check if there is such a redirect session created. If yes, redirect the user to that URL and delete the session.

1

Putting into practice the Idea of the Martian, in view of your comment. I suggest you do so:

<?php if (!isset($_SESSION)) session_start(); // Verifica se não há a variável da sessão que identifica o usuário
if ($_SESSION['nivelAcesso'] != 1) {

// Redireciona o visitante de volta pro login
    $_SESSION['destino'] = $_SERVER["REQUEST_URI"];
    header("Location: index.php");
    exit;
}
require_once "config.php";
   // Verificar se está logado, se não reencaminha para a página de login
?>

-1


I was able to solve the problem by including the next code on all pages (other than the login page) and the login page, respectively...

Regular pages:

if (!isset($_SESSION)) session_start();

  // Verifica se não há a variável da sessão que identifica o usuário
  if ($_SESSION['nivelAcesso'] != 1) {

      $_SESSION['destino'] = $_SERVER["REQUEST_URI"];
      // Redireciona o visitante de volta pro login
      header("Location: /"); 
      exit;
  }

Login page:

if(isset($_SESSION['destino']) && $_SESSION['destino'] != "") {
                                header("Location: ".$_SESSION['destino']);

                            }else{
                                if($_SESSION["nivelAcesso"] == "1"){
                                header("Location: geral");

                            }elseif($_SESSION["nivelAcesso"] == "2"){
                                header("Location: artesaos");

                            }else{
                                header("Location: voluntarios");

                            }
                            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.