简体   繁体   English

如何防止来自浏览器后退按钮的php页面?

[英]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. 我正在使用post方法将选定的变量传递到另一个页面。 Where I am getting the variables in the next page by $_POST() method. 我在哪里通过$ _POST()方法在下一页获取变量。

And I am updating the variables passed into the database, after updation giving javascript pop-up as "you have saved successfully". 在更新后,我将更新传递给数据库的变量,将javascript弹出窗口显示为“您已成功保存”。

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. 如何通过禁用浏览器后退按钮来防止这种情况。

Instead of disabling the back button, you could redirect the user if he gets back to the page using sessions. 如果用户使用会话返回到页面,则可以禁用该用户,而不是禁用“后退”按钮。

page1.php page1.php中

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

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 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 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 })

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. 然后,如果用户尝试返回,则查看$_SERVER['HTTP_REFERER']如果它是“ successful.php”,则不要保存数据。

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. 我真的很讨厌他们这样做或添加几个页面,而您必须单击这些页面才能退出页面。

You can have your post method open up a new tab so that there is no back navigation to go to: 您可以将post方法打开一个新选项卡,以便没有后退导航:

<!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. 目标将生成一个新窗口。如果要关闭旧窗口,请添加两个脚本,以在打开新选项卡后500毫秒关闭上一个选项卡。

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

  } 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM