重寄表格(F5)

时间:2018-09-02 13:37:38

标签: javascript php

有一个将POST请求发送给自己的请求。执行脚本后,一切都很好,但是当您按F5键时,脚本将再次运行。如何禁止呢?由于某种原因,重定向无法正常工作。

<?php
$img = $_FILES['img'];
$headerurl = $_SERVER['PHP_SELF']."?id=".$id;
if(isset($_POST)) { 
    if($img['name'] != '') {
        $filename = $img['tmp_name'];
        $client_id = "123456789";
        $handle = fopen($filename, "r");
        $data = fread($handle, filesize($filename));
        $pvars = array('image' => base64_encode($data));
        $timeout = 30;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
        $out = curl_exec($curl);
        curl_close($curl);
        $pms = json_decode($out, true);
        $url = $pms['data']['link'];
        $creator = $_SESSION['login'];
        if($url != ""){
            mysqli_query($link, "INSERT INTO gallery (steamid, url, creator) VALUES ('".$id."', '".$url."', '".$creator."')") or die(mysqli_error($link));
            //header('Location: ' . $headerurl); //not work
            //header('Location: https://google.com'); //not work
        } else {
            echo "<h4>Error: </h4>";
            echo $pms['data']['error'];  
        } 
    }
}
?>  

<form action="<?php echo $headerurl; ?>" enctype="multipart/form-data" method="POST">
    <label for="file-upload" class="custom-file-upload">
        <i class="fa fa-plus"></i>
    </label>
    <input name="img" type="file" alt="Add Photos" id="file-upload" onchange="this.form.submit()">
</form>  

3 个答案:

答案 0 :(得分:1)

您需要在后期处理完成后重定向页面

<?php
...
if(isset($_POST)) { 
    ....
    header('Location: '.$newURL);
}
?>  

答案 1 :(得分:0)

这不是正确的方法。但是将header("location: yourlocation/");重定向

答案 2 :(得分:0)

尝试使用此代码,兄弟...

<?php
$img = $_FILES['img'];
$headerurl = $_SERVER['PHP_SELF']."?id=".$id;
if(isset($_POST)){ 
    if($img['name'] != '') {
        $filename = $img['tmp_name'];
        $client_id = "123456789";
        $handle = fopen($filename, "r");
        $data = fread($handle, filesize($filename));
        $pvars = array('image' => base64_encode($data));
        $timeout = 30;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
        $out = curl_exec($curl);
        curl_close($curl);
        $pms = json_decode($out, true);
        $url = $pms['data']['link'];
        $creator = $_SESSION['login'];
        if($url != ""){
           $result =  mysqli_query($link, "INSERT INTO gallery (steamid, url, creator) VALUES ('".$id."', '".$url."', '".$creator."')");

            if($result){

                header("location: google.com");

            }else{

                echo "<h4>Error: </h4>";

            }

        } else {
            echo "<h4>Error: </h4>";
            echo $pms['data']['error'];  
        } 
    }
}
?> 

<form action="<?php echo $headerurl; ?>" enctype="multipart/form-data" method="POST">
    <label for="file-upload" class="custom-file-upload">
        <i class="fa fa-plus"></i>
    </label>
    <input name="img" type="file" alt="Add Photos" id="file-upload" onchange="this.form.submit()">
</form>