繁体   English   中英

重新加载后不发布

[英]Don't post after reload

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

<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种方式:

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

使用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秒钟后过期,并让他再次提交表单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM