how to prevent f5 resubmitting php page

只愿长相守 提交于 2019-12-14 03:32:59

问题


how to prevent f5 resubmitting php page?

i very tired,

i want prevent f5 !

i tried very much of solutions

but all solutions is invalid!

please help me

i tried header('Location: index.php'); but not working!

if you can help me, please edit my php code and paste your code..

My form code in index.php file:

<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:

<?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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/19624635/how-to-prevent-f5-resubmitting-php-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!