gpt4 book ai didi

php - 刷新页面或单击后退按钮时如何防止重新提交表单

转载 作者:可可西里 更新时间:2023-10-31 22:51:04 25 4
gpt4 key购买 nike

在我的表单上,我允许用户将文件上传到数据库,然后我将它们发送到另一个页面,即 submission_successful.php,上面写着“感谢您提交”。但我注意到,当我点击提交成功的 php 文件上的后退按钮时,它会返回到表单并且那里有相同的信息并允许再次提交。我想要做的是在点击后退按钮时终止代码,或者清除用户输入的所有内容。我找到了几个答案,比如使用 缓存控制,但是有些含糊不清,有些对我不起作用。另外,我不希望用户在成功页面上时返回到上传页面。所以这就是为什么我会为“注销”或“返回上传页面”创建 2 个按钮,如果它们点击返回按钮,它将崩溃。我想显示 Confirm Form Resubmission页。在其他帖子中,他们试图真正阻止“确认表格重新提交”,但为了安全起见,我想拥有它。这是我的代码

developerUpload.php

<?php

session_start();

if(array_key_exists("invalid", $_GET)){

echo '<br><h3 style="color:red;">File(s) were already submitted! Please re-name file or select a different file...</h3>';
}

if(isset($_COOKIE['username'])){

if($_SERVER['REQUEST_METHOD'] =="POST"){

$price = addslashes(trim($_POST['price']));
$description = addslashes(trim($_POST['description']));

if(!empty($price) && !empty($description)){

$userid = $_SESSION['id'];
$username = $_SESSION['username'];
echo '<br>'.$userid;
$pack_id = rand();

//Check file 1
if($_FILES['file1']['error'] !== UPLOAD_ERR_OK){

$file1 = null;
}else{

$target1 = "devFiles/";
$target_file1 = addslashes(trim($target1 . basename($_FILES["file1"]["name"])));
$file1 = addslashes(trim($_FILES['file1']['tmp_name']));

}

//Check file 2
if($_FILES['file2']['error'] !== UPLOAD_ERR_OK){

$file2 = null;
}else{

$target2 = "devFiles/";
$target_file2 = addslashes(trim($target2 . basename($_FILES["file2"]["name"])));
$file2 = addslashes(trim($_FILES['file2']['tmp_name']));

}

//Check file 3
if($_FILES['file3']['error'] !== UPLOAD_ERR_OK){

$file3 = null;
}else{

$target3 = "devFiles/";
$target_file3 = addslashes(trim($target3 . basename($_FILES["file3"]["name"])));
$file3 = addslashes(trim($_FILES['file3']['tmp_name']));

}

//Check file 4
if($_FILES['file4']['error'] !== UPLOAD_ERR_OK){

$file4 = null;
}else{

$target4 = "devFiles/";
$target_file4 = addslashes(trim($target4 . basename($_FILES["file4"]["name"])));
$file4 = addslashes(trim($_FILES['file4']['tmp_name']));

}

//Check file 5
if($_FILES['file5']['error'] !== UPLOAD_ERR_OK){

$file5 = null;
}else{

$target5 = "devFiles/";
$target_file5 = addslashes(trim($target5 . basename($_FILES["file5"]["name"])));
$file5 = addslashes(trim($_FILES['file5']['tmp_name']));

}

//Check video
if($_FILES['video']['error'] !== UPLOAD_ERR_OK){

$video = null;
$videoName = null;
}else{

$target = "devFiles/";
$target_file = addslashes(trim($target . basename($_FILES["video"]["name"])));
$video = addslashes(trim($_FILES['video']['tmp_name']));
$videoName = addslashes(trim($_FILES['video']['name']));

}

if(file_exists($target_file1)
or file_exists($target_file2)
or file_exists($target_file3)
or file_exists($target_file4)
or file_exists($target_file5)
or file_exists($target_file)){

header("Location: developerUpload.php?invalid");
exit;

}

if(move_uploaded_file($_FILES["file1"]["tmp_name"], $target_file1)
&& move_uploaded_file($_FILES["file2"]["tmp_name"], $target_file2)
&& move_uploaded_file($_FILES["file3"]["tmp_name"], $target_file3)
&& move_uploaded_file($_FILES["file4"]["tmp_name"], $target_file4)
&& move_uploaded_file($_FILES["file5"]["tmp_name"], $target_file5)
&& move_uploaded_file($_FILES["video"]["tmp_name"], $target_file)){

try{

// new php data object
$handler = new PDO('mysql:host=127.0.0.1;dbname=magicsever', 'root', '');
//ATTR_ERRMODE set to exception
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(PDOException $e){
die("There was an error connecting to the database");

}

header("Location: submission_successful.php?");
die();
}



}else{

echo '<br><h1 style="color:red;">VALUES MISSING!</h1>';

}
}
}else {

header("Location: developerLogin.php");
}



?>

提交成功.php

<?php
session_start();

if(array_key_exists("invalid", $_GET)){

header("Location: developerUpload.php?invalid");

}
if(isset($_COOKIE['username'])){
echo '<br><h1 style="color:red; text_align:center;">Thank You for Submitting!</h1>';

}else{

header("Location: developerLogin.php");
}

?>

最佳答案

我找了好几天,终于找到了一些东西。如果您使用 HTML 命令,它会在用户返回时删除用户输入的任何内容。因为我的问题是当用户在被重定向后返回时,他们的信息仍然存在但是如果你使用

<form method="post" enctype="multipart/form-data" autocomplete="off">

它删除了所有内容,所以有点帮助。用户仍将被允许返回,但至少现在他们不能重新提交数据。

关于php - 刷新页面或单击后退按钮时如何防止重新提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44991941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com