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