-2

This is a part of my payment.php code. $f=1 when the values got stored in database correctly.

if($f==1){
  echo "<script>alert('Rooms Successfully Reserved')</script>";
  echo "<script>window.location.replace('home.php')</script>";
}

Now we all know window.location.replace() replaces the current page, removing the previous one from the back button history.

But the back button is working.

I do not want the user to press the back button and enter the payment.php again. So, what should i do with it?

Same goes for my login.php page, it redirects to home.php but again one can press back button and get to login.php.

How should i prevent it?

I even tried this:

window.history.forward()

But even it isn't working.

Please help

PrathamJ
  • 162
  • 12
  • 2
    Does this answer your question? [How can I stop the browser back button using JavaScript?](https://stackoverflow.com/questions/12381563/how-can-i-stop-the-browser-back-button-using-javascript) – biesior Apr 20 '21 at 16:38
  • There's no one reliable solution, the semi-workaround is `window.onbeforeunload` to display a message, but it also can be skipped by the user. – biesior Apr 20 '21 at 16:39
  • One reason why you want to prevent this is that you're outputting alert() scripts in your PHP code. This is bad practice. (Using alert() as part of your UI in general could be said to be bad practice.) A better way is to handle the user pressing the back button by making sure it doesn't break your app's flow. Keep the current state of your payment process in the user's session, and simply redirect the user if they happen to navigate somewhere that doesn't make sense. If you want full control instead, create an SPA instead. –  Apr 20 '21 at 16:42
  • @biesior ```window.onbeforeunload``` will just ask user whether to go back or not, but i wanted it not to go back at all. Is there some way to not let payment.php load? – PrathamJ Apr 20 '21 at 16:45
  • That's why I called it *semi-workaround*, In general that can be tricky and will always be environment/browser dependadnd and I'd definitely go with Chris' advise. – biesior Apr 20 '21 at 16:48
  • @biesior thanks.. but it didn't work... what if i use ```if(isset($_SESSION['xyz'])){ header("location: home.php"); exit(); }``` **$_SESSION['xyz']** will be set to **f** in payment.php. So if payment is already made and user is in home.php now, if user tries to go back to payment.php it'll redirect him to home.php. Is this logic correct? – PrathamJ Apr 20 '21 at 17:01
  • @ChrisG actually I'm new at all html css php js, and this is for my college project. So, that's why I'm using alert() even though i don't want to, but I don't know any other method of throwing input errors or display successful message. – PrathamJ Apr 20 '21 at 17:03
  • @PrathamJ currently you can just display an error message or modal, depending on what frontend lib you use. – biesior Apr 20 '21 at 17:19
  • Exactly, a very simple alternative is just `echo "

    Rooms Successfully Reserved

    ";`
    –  Apr 20 '21 at 17:37
  • @biesior actually I'm using bootstrap templates, so the js script is a bootstrap link.. and ig that's why some of the functions aren't working properly – PrathamJ Apr 20 '21 at 18:12
  • @ChrisG it displays on the top of the page, above my navbar, and it kinda looks bad. I want it such that whenever one click submit, and there is a invalid input, it should display the error **beside** input. But, I'm unable to find it anywhere. – PrathamJ Apr 20 '21 at 18:18
  • "*it displays on the top of the page, above my navbar, and it kinda looks bad.*" What do you expect from us, that we will place elements on your own page for you??? Please put some effort. – biesior Apr 20 '21 at 18:20
  • Sounds like you want JavaScript validation instead. However we cannot hold your hand through beginner's problems like that; you have to learn the basics of JavaScript and implement something yourself. –  Apr 20 '21 at 21:12
  • __am i asking you to put elements on my page__ **??!!** – PrathamJ Apr 21 '21 at 02:41

1 Answers1

1

I think you can work with the Unload Event and the History Function.

<script type="text/javascript"> 
        function back() { 
            window.history.forward();  
        } 
          
        // Force Client to forward to last (current) Page.
        setTimeout("back()", 0); 
          
        window.onunload = function () { null }; 
</script>

Add this before your Body Tag gets closed.

Jan Heil
  • 395
  • 4
  • 13