如何防止f5重新提交php页面?
我很累,
我想要防止f5!
我尝试了很多解决方案
但所有解决方案都无效!
请帮帮我
我试过标题('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
}
}
?>
感谢。
答案 0 :(得分:1)
您可以在表单中将nonce设置为隐藏值,并在最初提供页面时将其存储在数据库中。收到第一个表单提交时,请将该nonce标记为已使用。然后,如果您收到更多具有相同nonce的提交,您将知道该表单已重新提交,因此您可以采取适当的措施。
答案 1 :(得分: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();
?>
答案 2 :(得分: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']);
}