Skip to content
Advertisement

php – prevent back button

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement