Prajna Hegde
Prajna Hegde

Reputation: 740

How to prevent a php page from browser back button?

I have a form which contains many drop-down and numeric slide-bar.

I am using post method to pass the selected variables to another page. Where I am getting the variables in the next page by $_POST() method.

And I am updating the variables passed into the database, after updation giving javascript pop-up as "you have saved successfully".

So my problem is when I click on browser back button, the values are getting updated in the database again and again. How can I prevent this by disabling browser back button.

Upvotes: 1

Views: 11793

Answers (4)

Reza Saadati
Reza Saadati

Reputation: 5439

Instead of disabling the back button, you could redirect the user if he gets back to the page using sessions.

page1.php

session_start();
if (isset($_SESSION['block'])) {
    header('Location: page2.php');
}

page2.php

session_start();
$_SESSION['block'] = true;

Another option:

This is how you could set values of all your input fields back, if the user clicks back:

page1.html

var block = localStorage.getItem("block");

window.addEventListener("beforeunload", function() {
    if (block === 1) {
        const block = true;
    }
});

if (block) {
    const inputs = document.getElementsByTagName('input');
    for (input of inputs) {
        input.value = '';
    }
}

page2.html

localStorage.setItem("block", 1);

In this case, if you don't want your values get updated in your database, use:

if (!empty($_POST['my_value']) { // Add to database })

Upvotes: 1

F5 Buddy
F5 Buddy

Reputation: 494

Whenever you post your data then you should check your post data that this is empty or not

<?php 
  if(isset($_POST) && !empty($_POST) ){

     //your code for update    
     //Then after run update query and  success message ,do empty the post
     $_POST=array(); // empty the post

  } 
?>

Upvotes: 0

Daniel Lord
Daniel Lord

Reputation: 792

You can have your post method open up a new tab so that there is no back navigation to go to:

<!DOCTYPE HTML>
<html>
<body>

<form action="www.google.com" method="post" target="_blank">
    <input type="submit">
</form>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$('#theSubmit').on('click', function () {
    setTimeout(function(){
        window.close();                
    }, 500);               
})

</script>

The target generates the new window And if you would like to close the old window add the two scripts that close the previous tab 500ms after the new tab is opened.

Upvotes: 1

Andreas
Andreas

Reputation: 23968

Don't disable the back button, fix the problem that the data is saved multiple times instead. You could use pass the users to a separate page with message "you have successfully...".

Then if the user tries to go back you look at $_SERVER['HTTP_REFERER'] if that is "successful.php" then don't save the data.

Disabling back buttons is a big reason for me to block a page so that I can't visit it again.
I truly hate when they do that or add several pages that you have to click back to get out of a page.

Upvotes: 0

Related Questions