-1

点击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
}
}
}
?>
4

1 回答 1

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");
}
?>
于 2013-11-07T02:07:55.507 回答