1

So I have this code. When I enter data and click submit button its okay and no problems. After I refresh the page, the data posting again and again. I don't want this to happen,I want to post data once.

<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>
Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
easy pz
  • 69
  • 9
  • 5
    Possible duplicate of [How to prevent form resubmission when page is refreshed (F5 / CTRL+R)](https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr) – CecilMerrell aka bringrainfire Jan 26 '19 at 22:24
  • 1
    Possible duplicate of [stop inserting data in the database when refreshing the page](https://stackoverflow.com/questions/18115665/stop-inserting-data-in-the-database-when-refreshing-the-page) – pmarkoulidakis Jan 26 '19 at 22:30
  • Check @Wiimm's answer – Soren Jan 26 '19 at 23:35

2 Answers2

1

Use a cookie to determine if the user has previously submitted the form and stop him from resubmitting for a determined period.

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

In case the user is coming back to the page and you want him to be able to submit the form again (before the 1h cookie expires), you should add an exception for this to remove the cookie and the code will look like this:

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

This last example will make the cokie expire in 1 second and will let him submit the form again.

TheoPlatica
  • 1,060
  • 7
  • 11
1

I use 2 ways:

  • Define a token when creating the form and mark it invalid after first submission.
  • Use a redirect after accepting the form data.
Wiimm
  • 2,971
  • 1
  • 15
  • 25