PHP prevents update retries

Basically, I created a page to send password recovery mail. The problem is that after filling out all the information and sending mail, it will continue to send letters every time you refresh the page. The only solution I can think of is to open another page with

header("Location: index.php");

Or something that would be nice, I think, but are there any other solutions? I found something about disabling all variables, for example, but I really don't know how viable is

+4
source share
5 answers

? , PHP- -. HTML, , , PHP .

PHP, HTML. , , , PHP , . :

send.php

<?php
session_start();
?>
<html>
  <head>
    <title>Session</title>
  </head>
  <body>
<?php
if ( IsSet( $_SESSION[ "message_sent" ] ) )
   { echo "Your message was sent.";
     unset( $_SESSION[ "message_sent" ] );
   }
?>
    <form method="post" action="email.php">
      Enter message
      <input type="text" name="anything" />
      <input type="submit" value="Send email" />
    </form>
  </body>
</html>

email.php

<?php
session_start();
// SEND MESSAGE HERE.
$_SESSION[ "message_sent" ] = "message sent";
header( "Location: send.php" );
?>

, send.php . . , , , PHP , .

, .

+2

, - .

session_start();
if(!isset($_SESSION['mail_sent'])) {
    mail('someaddresss'...);
    $_SESSION['mail_sent'] = true;
}
+2

, . PHP- , , , .

cookie - . . header("Location: http://www.somewhereelse.com/); . cookie .

, "" , , cookie .

+1

:

//form.php
<?php
if($_POST){
    //form processing code here
    //redirect to self
    header("Location: ./form.php");
    exit();
}
0
source

If you use the nonce field in the form, the same form submission will not be processed twice. The general idea is to create a token that can only be used once. When the form was submitted with a valid token, the token becomes invalid.

Creating nonces is pretty easy to do: How to create and use sheet music

0
source

All Articles