重新加载后不发布

时间:2019-01-26 22:20:20

标签: php html

所以我有这段代码。当我输入数据并单击提交按钮时,没有问题。 在刷新页面后,数据一次又一次地发布。我不希望发生这种情况,我只想发布一次数据。

<form action="" method="post">
Dogecoin-address:  <input type="text" name="address"/><br>
<input type="submit" name="submit" value="submit">
</form>
<?php    
if(isset($_POST['submit'])){ //check if form was submitted
if (isset($_POST['address'])) {
}

$url = 'example.com'. $_POST['address'];

 $data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...

$options = array(
'http' => array(
    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
    'method'  => 'POST',
    'content' => http_build_query($data)
)
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

//ob_start();
var_dump($result);
 //$output = ob_get_clean();


// later:  $message = "Success! You entered: hygtfrd".$input;
}    
 ?>


</body>
</html>

2 个答案:

答案 0 :(得分:1)

我使用2种方式:

  • 在创建表单时定义令牌,并在首次提交后将其标记为无效。
  • 接受表单数据后使用重定向。

答案 1 :(得分:0)

使用cookie来确定用户以前是否已提交表单,并在确定的时间内阻止他重新提交。

<form action="" method="post">
   Dogecoin-address:  <input type="text" name="address"/><br>
   <input type="submit" name="submit" value="submit">
</form>
<?php    
  if(isset($_POST['submit']) && !isset($_COOKIE['sumbission_cookie_name']) ){ //check if form was submitted & if it's a new submission

    setcookie('sumbission_cookie_name', TRUE, time() + (60*60), "/" ); // expires in 1h

    if (isset($_POST['address'])) {
    }

    $url = 'example.com'. $_POST['address'];

    $data = array('key1' => 'value1', 'key2' => 'value2');
    // use key 'http' even if you send the request to https://...

    $options = array(
                    'http' => array(
                       'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                       'method'  => 'POST',
                       'content' => http_build_query($data)
                    )
                  );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    if ($result === FALSE) { /* Handle error */ }

    //ob_start();
    var_dump($result);
    //$output = ob_get_clean();


    // later:  $message = "Success! You entered: hygtfrd".$input;
  }    
?>
</body>
</html>

如果用户返回页面,并且您希望他能够再次提交表单(在1h Cookie过期之前),则应为此添加一个例外,以删除该cookie,并且代码如下所示这个:

<form action="" method="post">
   Dogecoin-address:  <input type="text" name="address"/><br>
   <input type="submit" name="submit" value="submit">
</form>
<?php    
  if( isset($_POST['submit']) ){ //check if form was submitted

    if( !isset($_COOKIE['sumbission_cookie_name']) ) { // check if it's a new submission

      setcookie('sumbission_cookie_name', TRUE, time() + (60*60), "/" ); // expires in 1h

      if (isset($_POST['address'])) {
      }

      $url = 'example.com'. $_POST['address'];

      $data = array('key1' => 'value1', 'key2' => 'value2');
      // use key 'http' even if you send the request to https://...

      $options = array(
                      'http' => array(
                         'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                         'method'  => 'POST',
                         'content' => http_build_query($data)
                      )
                    );
      $context  = stream_context_create($options);
      $result = file_get_contents($url, false, $context);
      if ($result === FALSE) { /* Handle error */ }

      //ob_start();
      var_dump($result);
      //$output = ob_get_clean();


      // later:  $message = "Success! You entered: hygtfrd".$input;
    }
  }   
  else {    
    setcookie('sumbission_cookie_name', TRUE, time() + 1, "/" ); // expires in 1s
  } 
?>
</body>
</html>

最后一个示例将使可可豆在1秒钟后过期,并让他再次提交表单。