使用 PHP 表单提交返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19215637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:01:33  来源:igfitidea点击:

navigate back with PHP form submission

phphtmlforms

提问by Maxim Ershov

So here is the deal, I am using HTML forms to transfer variables from page to page and PHP script to create pages based on values submitted. In general it looks like this: from the catalog of items you select what you want and the next page shows details for this specific item. Everything works perfect, except one thing:

所以这就是交易,我使用 HTML 表单在页面之间传输变量,并使用 PHP 脚本根据提交的值创建页面。通常它看起来像这样:从项目目录中选择您想要的内容,下一页显示该特定项目的详细信息。一切都很完美,除了一件事:

Whenever I use browser's back button, I always get the error: ERR_CACHE_MISSand I need to refresh page and then confirm that I really want to resubmit data.

每当我使用浏览器的后退按钮时,总是出现错误: ERR_CACHE_MISS,我需要刷新页面,然后确认我确实要重新提交数据。

Is there any way to fix this, so my customers would be able just to use back button as they supposed to.

有什么办法可以解决这个问题,这样我的客户就可以按照他们的意愿使用后退按钮了。

Here is the full text that browser provides me:

这是浏览器提供给我的全文:

This webpage requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. Reload this webpage. Press the reload button to resubmit the data needed to load the page. Error code: ERR_CACHE_MISS

此网页需要您之前输入的数据才能正确显示。您可以再次发送此数据,但这样做将重复此页面之前执行的任何操作。重新加载此网页。按重新加载按钮重新提交加载页面所需的数据。错误代码:ERR_CACHE_MISS

回答by user3877861

When you post forms with php, or any other data, you may come back to the page and find a message in the browser like "Document Expired" or "Confirm Form Resubmission With Chrome". These messages are a safety precaution the browser uses with sensitive data such as post variables. The browser will not automatically give you the fresh page again. You must reload the page by clicking try again or with a page refresh. Then, it operates as you would expect it to.

当您使用 php 或任何其他数据发布表单时,您可能会返回该页面并在浏览器中找到一条消息,例如“文档已过期”或“使用 Chrome 确认重新提交表单”。这些消息是浏览器对敏感数据(如帖子变量)使用的安全预防措施。浏览器不会再次自动为您提供新页面。您必须通过单击重试或刷新页面来重新加载页面。然后,它会按照您的预期运行。

However, the php coder can work around the annoying message from the browser by adding a little code into the script. The example shows a couple of lines of code that can be added above session_start() in order to be able to go back and forth to the page when you post without any hangups.The 'private_no_expire' mode means that the client will not receive the expired header in the first place.

但是,php 编码器可以通过在脚本中添加一些代码来解决来自浏览器的烦人消息。该示例显示了几行代码,可以在 session_start() 上方添加这些代码,以便在您发布时无需挂断即可来回页面。“private_no_expire”模式意味着客户端将不会收到首先是过期的标头。

header('Cache-Control: no cache'); //no cache
session_cache_limiter('private_no_expire'); // works
//session_cache_limiter('public'); // works too
session_start();

回答by Medeno

**Some background: Credit goes to bruce (sqlwork.com) for his excellent explanation.

** 一些背景:归功于 bruce (sqlwork.com) 的出色解释。

This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. Press Reload to resend that data and display this page.

此网页需要您之前输入的数据才能正确显示。您可以再次发送此数据,但这样做将重复此页面之前执行的任何操作。按重新加载重新发送该数据并显示此页面。

Because of the sloppy coding practices of web developers browsers were forced to add this message. the scenario is as follows:

由于 Web 开发人员的编码实践草率,浏览器被迫添加此消息。情况如下:

1) user fills in form and submits (posts form) 2) the server process the post data and responds with a new page (confirm) marked as not cacheable 3) the user navigates to a new page. 4) the user press back:

1)用户填写表单并提交(发布表单)2)服务器处理发布数据并以标记为不可缓存的新页面(确认)响应3)用户导航到新页面。4)用户按回:

for the the browser to display the page in step 2, because its marked no-cache, it must request it from the server, in other words do the repost of the data (do step 1). here is were the sloppy coding came in, if this was an credit card charge, and repost detection was not on the server, the card is charged twice. this was (is) so common a problem, that the browsers had to detect this and warn the users.

为了让浏览器在步骤 2 中显示页面,因为它被标记为 no-cache,所以它必须从服务器请求它,即重新发布数据(执行步骤 1)。这是马虎编码进来的,如果这是信用卡收费,并且重新发布检测不在服务器上,则卡会收费两次。这是一个非常普遍的问题,浏览器必须检测到这一点并警告用户。

the best fix is in step two, the server sends a redirect to the confirm page. then when the user accesses the confirm via history or back, its a get request, not a post request and will not show the warning.

最好的解决方法是在第二步,服务器发送一个重定向到确认页面。然后当用户通过历史记录或返回访问确认时,它是一个获取请求,而不是一个发布请求,并且不会显示警告。

note: webform's postback model lends itself to this problem. also avoid server transfers.

注意:webform 的回发模型适用于这个问题。还要避免服务器传输。

My solution

我的解决方案

$_SESSION['home'] used to store any errors on home page.

$_SESSION['home'] 用于存储主页上的任何错误。

$_SESSION['tempEmail'] used to echo value on php form.

$_SESSION['tempEmail'] 用于在 php 表单上回显值。

Note: Use one unique session variable for each page that has a HTML form for error handling and also any session variable for each value that is echoed on HTML form.

注意:为每个具有用于错误处理的 HTML 表单的页面使用一个唯一的会话变量,并为在 HTML 表单上回显的每个值使用任何会话变量。

<?php
session_start(); 

//Initialize variables not initialized without overwriting previously set variables.
if(!isset($_SESSION['home'])) {
$_SESSION['home']="";
$_SESSION['tempEmail']="";
}

Optional - If logged in, assign email address to the $_SESSION['tempEmail'] variable (if not previously done) to pre-fill HTML form.

可选 - 如果已登录,请将电子邮件地址分配给 $_SESSION['tempEmail'] 变量(如果之前未完成)以预填充 HTML 表单。

if(isset($_POST['Submit'])){

---your code---


    //Error message(s) examples
    $_SESSION['home'] = "Email and Password do not match, please try again.";
    header("Location: " . $_SERVER['REQUEST_URI']);

    $_SESSION['home'] = "Email address format is invalid.  Please recheck.";
    header("Location: " . $_SERVER['REQUEST_URI']);

    //success
    unset ($_SESSION['home']); //optional, unset to clear form values.
    header ("location: nextpage.php"); 
             ---or---
    header("Location: " . $_SERVER['REQUEST_URI']);  //re-post to same page with the $_SESSION['home'] success message.   
}
?>



<body>

Error box

错误框

<span><strong class="error"><?php echo $_SESSION['home'] ?></strong></span>

HTML form

HTML 表单

<form action="#" name="loginform" method="post" >
    <input type="text" name="userEmail" maxlength="50" title="Enter Your email" autocomplete="off" value="<?php echo htmlspecialchars($_SESSION['tempEmail']); ?>" placeholder="enter email" required/>
    <input type="submit" name="Submit" value="Submit">
</form>

</body>

Not recommended to use on payment page,see discussion above. Tested in Firefox, Chrome, Safari, and IE9. The annoying messages are gone when using back button. Ensure that output buffering is turned "on" in your php script or php.ini to avoid header warnings. You can check your php.ini file for the following;

不建议在支付页面上使用,见上面的讨论。在 Firefox、Chrome、Safari 和 IE9 中测试。使用后退按钮时,烦人的消息消失了。确保在您的 php 脚本或 php.ini 中“打开”输出缓冲以避免标题警告。您可以检查您的 php.ini 文件以了解以下内容;

output_buffering=On

回答by Mimouni

I found that using just :

我发现只使用:

header('Cache-Control: no cache'); //disable validation of form by the browser

resolve the problem

解决问题

回答by user5497991

I tried this answer and it's ok. You have to put this code before: session_start():

我试过这个答案,没关系。你必须把这段代码放在之前session_start()::

session_cache_limiter('private, must-revalidate');
session_cache_expire(60);

Good luck

祝你好运

回答by Craig Travis

<?php
if(isset($_POST['submit']))
{
 //submission goes here
}
?>

Is this what you were thinking?

这是你想的吗?

edit - SQL really is a beautiful thing to work with, I see it's been added as a recommendation in a comment, and I concur to use SQL if you can, its fast, intuitive and efficient.

编辑 - SQL 确实是一个很好用的东西,我看到它已被添加为评论中的推荐,如果可以,我同意使用 SQL,它快速、直观且高效。

回答by Kapytanhook

None of the other answers worked for me.

其他答案都不适合我。

  • I don't want to redirect
  • Setting different headers didn't work
  • I already use tokens in my post to ensure re-submission can't happen
  • I post to the same url the form is showing on
  • 我不想重定向
  • 设置不同的标题不起作用
  • 我已经在我的帖子中使用了令牌以确保不会发生重新提交
  • 我发布到表单显示的同一个网址

This simple javascript fixes my issue of the back button throwing "ERR_CACHE_MISS"

这个简单的javascript解决了我的后退按钮抛出“ERR_CACHE_MISS”的问题

if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}