Home > Back-end >  PHP Redirect user and save previous URL in browser's history
PHP Redirect user and save previous URL in browser's history

Time:10-19

This got me quite confused. Let me say a user visits a folder A, but, let's say, because of some reason he is being redirected to a folder B. The problem is that a browser does not log the location A into its history.

url/A redirects to a little different location url/B

After user clicks on "history back button", he does not reach the previous url/A location. It's kinda omitted, I'd say.

I've tried these 2 methods for the redirect. And it seems they are not saving the current URL into the browser's history.

header("Refresh:0; url=$url_b");
header('Location: '.$url_b); 

CodePudding user response:

Although I'm still questioning the reason, but you maybe able to use javascript on page A for redirection. In this way we are able to get back to where we were on back click.

So here we have a js script that only runs if a php condition is false:

<?php
if ( ! condition) { ?>
    <script>
        window.location.href = "http://example.com/B";
    </script>
<? }
?>
  • Related