简体   繁体   English

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

[英]how to prevent f5 resubmitting php page

how to prevent f5 resubmitting php page?如何防止f5重新提交php页面?

i very tired,我很累,

i want prevent f5 !我想阻止 f5 !

i tried very much of solutions我尝试了很多解决方案

but all solutions is invalid!但所有解决方案都无效!

please help me请帮我

i tried header('Location: index.php');我试过 header('Location: index.php'); but not working!但不工作!

if you can help me, please edit my php code and paste your code..如果你能帮助我,请编辑我的 php 代码并粘贴你的代码..

My form code in index.php file:我在 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>

My php code in upload.php file:我在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
}
}
?>

Thanks.谢谢。

You could set a nonce as a hidden value in the form, and store it in your database when you initially serve the page.您可以在表单中设置一个随机数作为隐藏值,并在您最初为页面提供服务时将其存储在您的数据库中。 When you receive the first form submission, mark that nonce as used.当您收到第一个表单提交时,将该随机数标记为已使用。 Then if you receive more submissions with the same nonce, you'll know the form has been resubmitted so you can take appropriate action.然后,如果您收到更多具有相同随机数的提交,您就会知道该表单已被重新提交,以便您可以采取适当的措施。

hope this help.希望这有帮助。

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

Try this;尝试这个;

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']); 
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM