单击f5后如何防止重新提交

时间:2013-11-07 01:52:49

标签: php forms

点击f5后如何防止重新提交

我想上传后防止f5 php

我尝试header();include '';

但没有工作!

任何解决方案?

我的表格

<form id="pic-form" enctype="multipart/form-data" action="pic.php" method="POST">
<input name="uploadedfile" id="pic-upload" type="file"/>
<input type="submit" value="upload" id="submit" name="submit"/>
</form>

我的PHP代码

<? include 'var.php';
if(isset($_POST['submit']) and $_SERVER['REQUEST_METHOD'] == "POST"){
if($terms == 'yes'){
if ((($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpg")
|| ($_FILES["uploadedfile"]["type"] == "image/x-png")
|| ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/png"))
&& ($_FILES["uploadedfile"]["size"] < $max_file_size)
&& in_array($extension, $formats))
{   
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], 
$uploaded );
echo '<img src="'.$url.$uploaded.'"/>';
echo '<h3>'.$url.$short.'</h3>';
}
elseif(!empty ($_FILES["uploadedfile"]["error"])){
echo '<h3>Please choose file to upload it!</h3>'; // if you don't choose file
}
elseif(!in_array($extension, $formats)){
echo '<h3>This extension is not allowed!</h3>'; // if you choose file not allowed
}
elseif($_FILES["uploadedfile"]["size"] = $max_file_size ){
echo "Big size!"; // if you choose big file
}
}
}
?>

1 个答案:

答案 0 :(得分:0)

无法直接阻止这种情况。您可以让PHP表单处理程序使用header()将页面重定向到没有向其发布数据的结果页面。如果用户刷新该页面(F5),则没有重新发布。

这也意味着您必须在提交表单之前验证表单,或者通过URL传递结果。例如:

<? include 'var.php';
if(isset($_POST['submit']) and $_SERVER['REQUEST_METHOD'] == "POST"){
  if($terms == 'yes'){
    if ((($_FILES["uploadedfile"]["type"] == "image/jpeg")
    || ($_FILES["uploadedfile"]["type"] == "image/jpg")
    || ($_FILES["uploadedfile"]["type"] == "image/x-png")
    || ($_FILES["uploadedfile"]["type"] == "image/gif")
    || ($_FILES["uploadedfile"]["type"] == "image/png"))
    && ($_FILES["uploadedfile"]["size"] < $max_file_size)
    && in_array($extension, $formats)){   
      move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $uploaded );
      $src = $url.$uploaded;
      $short = $url.$short;
    }
    elseif(!empty ($_FILES["uploadedfile"]["error"])){
      $error = '<h3>Please choose file to upload it!</h3>'; // if you don't choose file
    }
    elseif(!in_array($extension, $formats)){
      $error = '<h3>This extension is not allowed!</h3>'; // if you choose file not allowed
    }
    elseif($_FILES["uploadedfile"]["size"] = $max_file_size ){
      $error = "Big size!"; // if you choose big file
    }
  }
}
if(isset($error)){
  header("location: errorpage.php?e=$error");
}else {
  header("location: successpage.php?sr=$src&sh=$short");
}
?>