【问题标题】:Don't post after reload重新加载后不要发布
【发布时间】:2019-06-20 08:31:07
【问题描述】:

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

<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>

【问题讨论】:

标签: php html


【解决方案1】:

使用 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>

最后一个例子会让cokie在1秒后过期,让他再次提交表单。

【讨论】:

    【解决方案2】:

    我使用两种方式:

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

    【讨论】:

    • 这些是最好的解决方案:)
    猜你喜欢
    • 2019-05-19
    • 2016-10-26
    • 2017-07-07
    • 2011-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多