-5

如何防止f5重新提交php页面?

我很累,

我要阻止f5!

我尝试了很多解决方案

但所有解决方案都是无效的!

请帮我

我试过 header('Location: index.php'); 但不工作!

如果你能帮助我,请编辑我的 php 代码并粘贴你的代码..

我在 index.php 文件中的表单代码:

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

我在 upload.php 文件中的 php 代码:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "zip", "pdf", "docx", "rar", "txt", "doc");
$temp = explode(".", $_FILES["uploadedfile"]["name"]);
$extension = end($temp);
$newname = $extension.'_'.substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 7)), 4, 7);
$imglink = 'imgs/file_';
$uploaded = $imglink .$newname.'.'.$extension;
if(isset($_FILES["uploadedfile"])){
header('Location: index.php'); 
if ((($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpg")
|| ($_FILES["uploadedfile"]["type"] == "image/pjpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/x-png")
|| ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
|| ($_FILES["uploadedfile"]["type"] == "application/msword")
|| ($_FILES["uploadedfile"]["type"] == "text/plain")
|| ($_FILES["uploadedfile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["uploadedfile"]["type"] == "application/pdf")
|| ($_FILES["uploadedfile"]["type"] == "application/x-rar-compressed")
|| ($_FILES["uploadedfile"]["type"] == "application/x-zip-compressed")
|| ($_FILES["uploadedfile"]["type"] == "application/zip")
|| ($_FILES["uploadedfile"]["type"] == "multipart/x-zip")
|| ($_FILES["uploadedfile"]["type"] == "application/x-compressed")
|| ($_FILES["uploadedfile"]["type"] == "application/octet-stream"))
&& ($_FILES["uploadedfile"]["size"] < 5242880) // Max size is 5MB
&& in_array($extension, $allowedExts))
{   
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
$uploaded );
include 'upload.html';
}
if($_FILES["uploadedfile"]["error"] > 0){
echo '<h3>Please choose file to upload it!</h3>'; // If you don't choose file
}
elseif(!in_array($extension, $allowedExts)){
echo '<h3>This extension is not allowed!</h3>'; // If you choose file not allowed
}
elseif($_FILES["uploadedfile"]["size"] > 5242880){
echo "Big size!"; // If you choose big file
}
}
?>

谢谢。

4

3 回答 3

1

您可以将nonce设置为表单中的隐藏值,并在您最初提供页面时将其存储在数据库中。当您收到第一个表单提交时,将该 nonce 标记为已使用。然后,如果您收到更多具有相同 nonce 的提交,您将知道该表单已被重新提交,因此您可以采取适当的措施。

于 2013-10-27T23:00:18.650 回答
0

尝试这个;

session_start();
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {       
  $_SESSION['postdata'] = $_POST;
  header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']); exit;
}

if( isset($_SESSION['postdata'])) { 
  $_POST = $_SESSION['postdata'];      
  unset($_SESSION['postdata']); 
}

资料来源:https ://vatanlar.com.tr/en/php-repost-prevention/

于 2021-06-04T19:23:36.823 回答
0

希望这有帮助。

<?php
if (version_compare(phpversion(), '5.4.0', '<')) {
    if(session_id() == '') {
            session_start();
    }
}else{
    if (session_status() == PHP_SESSION_NONE) {
            session_start();
    }
}
$sec = 2;//cooldown 2 sec
if(isset($_SESSION["time"]) && $_SESSION["time"] > 0 && (time() - $_SESSION["time"]) < $sec){
    //sleep($sec);
    die();
}
$_SESSION["time"] = time();
?>
于 2016-04-09T00:22:39.303 回答