当我的网页做了后,我存储所有的$_POST
在单独的数据$_SESSION
变种。 我知道,一个后退按钮此相同的页面是由设计来显示Document Expired
消息。 我的希望是到浏览器误以为,有真的从来没有任何$_POST
数据等不显示Document Expired
回来,就当消息。 我迫使页面的完整刷新,所以我并不担心接收旧数据,因为我把它保存在session。
我曾试图unset($_POST)
希望这会留在页面上。 但$_POST
数据必须被缓存/存储,并刷新或后退按钮返回。 正是我试图做可能吗? 有任何想法吗?
*更新*我的解决方案/回答是贴在下面。 它发布到一个单独的形式,其重定向回原来的形式进行处理。 不知道为什么下投票。 已经好几个月伟大的工作,我不再接收文件过期的消息。 它也可以防止重复张贴。
您可以使用后做到这一点 - 改 - 获取设计模式。
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);
您可以添加以下到脚本的开头来解决这个问题。
header("Cache-Control: max-age=300, must-revalidate");
最简单的解决方案,想到什么? 不要直接做后,赶上活动,并通过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是否做服务器端,但无论哪种方式,你必须做一些额外的工作,以“记住”的帖子的结果相同。
* UPDATE *这里是我的解决方案。
据我所知,与重定向后得到这个职位,通常是重新就业,为处理在同一页与一到达另一个目的地重定向从形式离开之前。 不过,我需要能够回到原来的页面重新编辑(如在文件模式,用户可以保存正在进行的工作)。 因此做POST到第二页,并重定向回到原来是我的摆脱了“过期”的消息,因为编辑形式不会有与它相关的数据后的想法。 我已经扩大了这一(未显示)(使用它与例如包括$ _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);
}
?>