刷新后防止表单重新提交

时间:2013-12-07 16:00:06

标签: php html

我一直在阅读关于此的其他问题,我应该使用标题('Locaction:xxx.php');但我无法弄清楚如何将其实现到我的代码中。对此我很抱歉。任何帮助或指导我将是最伟大的!这是下面的index.php:

<body>
<div id="container">
    <div id="upload">
        <div id="logo"><a href="index.php"><img src="images/logo.png"></a></div>
        <form enctype="multipart/form-data" method="post" action="uploader.php">
        <p class="uploadtxt">Choose your file below:</p>
        <input type="file" name="image" class="button" />
        <input type="submit" value="Upload It!" class="button" />
        </form>
    </div>
<?php include 'footer.php'; ?>
</div>
</body>
</html>

这是下面的uploader.php代码:

<?php
// Set local PHP vars from the POST vars sent from our form using the array
// of data that the $_FILES global variable contains for this uploaded file

$fileName = $_FILES["image"]["name"]; // The file name
$fileTmpLoc = $_FILES["image"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["image"]["type"]; // The type of file it is
$fileSize = $_FILES["image"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["image"]["error"]; // 0 for false... and 1 for true

$url = "http://localhost/";

// Specific Error Handling if you need to run error checking

if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 10000000) { // if file is larger than we want to allow
    echo "ERROR: Your file was larger than 10000000kB in file size.";
    unlink($fileTmpLoc);
    exit();
} else if (!preg_match("/.(gif|jpg|jpeg|png)$/i", $fileName) ) {
     // This condition is only if you wish to allow uploading of specific file types    
     echo "ERROR: Your image was not .gif, .jpg, .jpeg or .png.";
     unlink($fileTmpLoc);
     exit();
}

//-- GENERATE A RANDOM NAME --//

$newfilename = rand(0, 999);
$newerfilename = $newfilename .'-'. $fileName;

//-- MAKE UPLOADS FOLDER IN YEAR AND MONTHLY --//

$path = "uploads/";

$year_folder = $path . date("Y");
$month_folder = $year_folder . '/' . date("m");

!file_exists($year_folder) && mkdir($year_folder , 0777);
!file_exists($month_folder) && mkdir($month_folder, 0777);

$path = $month_folder . '/';

move_uploaded_file($_FILES["image"]["tmp_name"], $path . $newerfilename);

?>

<html>
<head>
<title>Localhost - Upload Completed!</title>

<?php include_once 'header.php'; ?>

<body>
<div id="container">
    <div id="upload">
        <div id="logo"><a href="index.php"><img src="images/logo.png"></a></div>
        <p class="filenametxt"><?php echo "The image is now uploaded!"; ?></p>
        <p class="uploadtxt">Get the link below:</p>
        <pre><?php echo $url . $path . $newerfilename; ?></pre>

    </div>
<?php include 'footer.php'; ?>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个:

    <input type="hidden" name="key" value="<?php echo (isset($_POST['key']) ? $_POST['key'] : rand(1,150)); ?>" />
    <?php if (isset($_POST['key']) { $_SESSION['key'] = $_POST['key']); } ?>

在你提交的PHP中:

<?php if (isset($_SESSION['key'])) { if ($_POST['key']==$_SESSION['key']){ echo "You may not resubmit a form!"; } } ?>