Back 2 pages with php / create session from last accessed URL

Asked

Viewed 499 times

1

I would like to know how to create a session of the last accessed url, why this: I have a login system on the site that if the person clicks to buy the product and is not logged in, it is directed to the login page, after logging in, it is redirected to the home with a redirect: redirect('/user/painel'); But I need that after she logs in, she goes back to the last URL accessed so she can buy the product she clicked on. Or maybe some Location header that goes back two pages. Note: The framework I’m using is Codeigniter I tried to use too: header("Location: " . $_SERVER['HTTP_REFERER'] . ""); but it only goes back one page, I need it to go back two.

  • 1

    Doesn’t that solve? $_SESSION['ultimaUrl'] = $_SERVER['REQUEST_URI'];

  • Not really, because the last url accessed is the login page.. actually need to go back to the second previous url, example: product clicking/login/login page - need to go back to product clicking not to login page

  • I think using Eduardo’s idea would be in the login page you check if there is something in this ultimaUrl and then when the user clicks on you or sends it to your user/painel or you send it to that last url.

  • If the application is like this, there should not be redirect to the home, but to the page that "requested" the login. One of the simplest ways is to pass the return to the URL. http://paginadelogin.php?retorno=/produto012.php - If the return is blank (or contains invalid characters and/or does not start with a bar which may indicate a forged redirect), redirect to home, or redirect to return.

  • Is that what happens, I have several products, and in the internal of each has the buy button, if the person is not logged in and click buy, it is directed to the login page, making the login it is directed to the home of the site, only that I wanted instead of her going back home, she goes back to the page where she was, that is, two previous pages, because the previous page is the login form: product internal / login page / logged in. if I give a Location to go back to the last page, it ends up falling on the login page.

  • 1

    So I think it’s going to be a harder solution simpler than the one I posted in the previous comment...

Show 1 more comment

1 answer

2


As seen, I used a session:

$_SESSION['url_assine'] = base_url().'assinatura'.$id;
    redirect('erro/efetuar_login_assinatura');

and inside the error/effective_login_signature controller I take the session I set:

 redirect($_SESSION['url_assine']);

Browser other questions tagged

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