【问题标题】:how to prevent f5 resubmitting php page如何防止f5重新提交php页面
【发布时间】:2013-11-06 15:00:46
【问题描述】:

如何防止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
}
}
?>

谢谢。

【问题讨论】:

标签: php forms submit refresh


【解决方案1】:

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

【讨论】:

  • 这比破坏浏览器功能会损害可用性要好得多。
【解决方案2】:

希望对您有所帮助。

<?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();
?>

【讨论】:

    【解决方案3】:

    试试这个;

    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/

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 2011-09-13
      • 2017-05-22
      • 2020-10-16
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      相关资源
      最近更新 更多