当我的网页发布帖子时,我会将所有$_POST
数据存储在单独的$_SESSION
var中。我知道同一页面的后退按钮是设计用来显示Document Expired
消息。我希望愚弄浏览器,认为确实没有任何$_POST
数据,因此在返回时不显示Document Expired
消息。我正在强制完全刷新页面,所以我不担心接收旧数据,因为我将它存储在会话中。
我曾试图unset($_POST)
希望这会留在网页上。但$_POST
数据必须缓存/存储,并返回刷新或返回按钮。我正在尝试做什么?有什么想法吗?
*更新* 我的解决方案/答案发布在下面。它发布到一个单独的表单,该表单重定向回原始表单进行处理。不确定为什么投票。它已经工作了好几个月,我不再收到Document Expired消息。它还可以防止重复发布。
答案 0 :(得分:4)
您可以使用post-redirect - get设计模式实现此目的 http://en.wikipedia.org/wiki/Post/Redirect/Get
实现此模式的更标准方法是在一个页面上显示,验证和保存表单。
以下解决方案具有以下优点:
1.所有与表单相关的代码都在一个地方。
2.服务器端验证很简单
我有两个页面,form.php和after.php。
form.php:
if(isPosted()){
if(dataIsValid($postedData)){
// dataIsValid should set $message to show to the user
saveData($postedData);
redirect('after.php');
die();
}
} else {
$postedData = $defaultValues;
}
showForm($postedData, $message);
答案 1 :(得分:2)
您可以将以下内容添加到脚本的开头以解决此问题。
header("Cache-Control: max-age=300, must-revalidate");
答案 2 :(得分:0)
想到最简单的解决方案?不要直接做帖子,抓住事件并通过AJAX提交表单。然后成功,重定向。
使用jQuery的一个例子:
$('#some_form').submit(function() {
$.post($(this).attr('action'), $(this).serialize(), function() {
window.location = "/some/success/url.php";
});
return false; // Prevent the form submission & any other events triggered on submit
});
由于POST从未添加到浏览器历史记录中,因此您不会遇到此问题。
但是,请注意POST网址现在与您加载的网址不同;您可以通过检查POST或GET是否在服务器端完成来实现相同,但不管怎样,您必须做一些额外的工作来“记住”POST的结果。
答案 3 :(得分:0)
*更新* 这是我的解决方案。
我知道通过post-redirect-get,POST通常会返回到同一页面进行处理,然后再从具有GET的表单重定向到另一个目标。但是,我需要能够返回原始页面进行重新编辑(如在用户可以保存正在进行的工作的文档模型中)。因此,将POST发送到第二页并重定向回原始文件是我想要删除“EXPIRED”消息,因为编辑表单不会有与之关联的帖子数据。我已经扩展了这个(未显示)以包含$ _FILE和其他情况(例如同时使用a href
)。不确定为什么要downvote。这已经好几个月了,并完成了任务。我不再收到“文件已过期”消息。此外,所有$ _POST处理都在原始文件中完成。
testform.php
<?php
session_start();
if (isset($_GET) && count($_GET)>0){
// process get
var_dump($_GET);
}
if (isset($_SESSION['post-copy'])){
// return post vars to $_POST variable so can process as normal
$_POST = $_SESSION['post-copy'];
// unset the session var - with refresh can't double process
unset($_SESSION['post-copy']);
// process post vars
var_dump($_POST);
}
?>
<form method='post' action='__b.php?redirect=<?php echo $_SERVER['PHP_SELF'] ?>&help=me' enctype='multipart/form-data'>
<textarea name='descr' id='descr'>ABCD</textarea>
<input type='submit' value='Go'>
</form>
redirect.php
<?php
if (!isset($_SESSION)){
session_start();
}
if (isset($_POST)){
$_SESSION['post-copy'] = $_POST;
}
// retrieve the url to return to
if (isset($_GET['redirect'])){
$url = $_GET['redirect'];
}
// if multiple query string parameters passed in get, isolate the redirect so can build querystring with the rest
if (isset($_GET) && count($_GET) > 1){
$get = $_GET;
foreach ($get as $key => $val){
if ($key == 'redirect'){
// remove from rest of passed get query string
unset($get[$key]);
}
}
if (count($get) > 0){
$url .= (strpos($url,"?")===false ? "?" : "&") . http_build_query($get);
}
}
if ($url!==""){
header("Location: " . $url);
}
?>