Reputation: 718
Basically I created a page to send a mail to restore your password. The problem is that after you fill in all the information and it has sent the mail it will continue to send emails everytime you refresh the page. The only solution I can think of is to open a different page with
header("Location: index.php");
Or something which would be fine I guess but are there other solutions? I found something about unsetting all variables for instance but I really don't know how viable that is
Upvotes: 5
Views: 2501
Reputation: 363
Possible solution would be to have two pages. One with PHP code in it that sends the email that user and another that says the email was sent.
Use a cookie on the first webpage with the email. Immediately once that page loads, it sends the email. You next use the header("Location: http://www.somewhereelse.com/);
to send the user to another page. On the next page, it removes the cookie or changes it.
That way, if the user clicks the back button or the refresh, it doesn't send the email again because the email cookie is missing.
Upvotes: 1
Reputation: 10371
Crecket, may we see your code? Because I'm affraid you are adding your PHP code into your webpage. When you put the action into the HTML file, everytime the page reloads, the PHP code is re-executed.
To prevent the PHP code from be re-executed, you have to separate it from the HTML files. This way, no matter how many times the user refreshes the page, the PHP code won't execute unless the user presses the submit button. I will give you and example of this separation :
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" );
?>
Copy and paste previous codes in two files with the given names, then run send.php from your browser. Send one message. Then refresh the page as many times as you want and you will see, the PHP code won't reexecute so the email won't be resent.
Hope this helps you.
Upvotes: 4
Reputation: 31614
Like Fred said, sessions are good solution here. Pseudo code below
session_start();
if(!isset($_SESSION['mail_sent'])) {
mail('someaddresss'...);
$_SESSION['mail_sent'] = true;
}
Upvotes: 2
Reputation: 6976
If you use a nonce field in the form, the same form submission will not be processed twice. The general idea is to generate a token that can only be used once. Once a form has been submitted with a valid token, the token becomes invalid.
Creating nonces is fairly easy to do: How to create and use nonces
Upvotes: 1
Reputation: 20469
You can make a redirect without actually using seperate files:
//form.php
<?php
if($_POST){
//form processing code here
//redirect to self
header("Location: ./form.php");
exit();
}
Upvotes: 0