how to prevent f5 resubmitting php page

2019-10-02 01:39发布

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.

2条回答
Deceive 欺骗
2楼-- · 2019-10-02 02:00

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();
?>
查看更多
冷血范
3楼-- · 2019-10-02 02:02

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.

查看更多
登录 后发表回答