0

I would like to know how I can get redirected to the previous page that I was looking at. Let's say I want to go to a page that requires sign in and I get redirected to login and after login I need to get backto that page iinstead of being redirected to home

  • this may help https://developer.mozilla.org/en-US/docs/Web/API/History_API – varontron Sep 30 '16 at 03:28
  • For anyone post history api, that is bad, android 3 and 4, IE 7, 8, 9 do not support it and no opera mini versions support it. – M H Sep 30 '16 at 04:28

1 Answers1

0

The way everyone does this is to pass the previous url to the new page as a querystring param.

This is very basic, you did not post code so I made some up. Make sure you validate and sanitize anything coming from GET or POST. But at least you can be sure it will be set instead of the httpreferrer that was suggested.

For example, if you redirect them to the login page with JavaScript.

if(LoggedIn()==FALSE){

    window.location.href = "login.php?pCurrentUrl=" + window.location.href;
}

Then on the login.php page you can log them in and redirect them to the.

gfRedir($_GET['pCurrentUrl']);



function gfRedir($_NewPath){
    header('Location: '.$_NewPath);
    exit();
}
M H
  • 2,179
  • 25
  • 50