Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Use PHP to send a contact form email only once

Posted on 2010-01-12
Medium Priority
Last Modified: 2013-12-13
I have a e-mail contact form written php and html.
On the first page quick_quote.php you input your contact data.
The quick-quote.php  page has the the e-mail script.
Once the submit button is pressed it directs you to the thank-you.php  page.   The thank-you.php page displays the information the client filed out on the previous page.
The variables are transfered from one page to another by $_POST.
My problem is if the client clicks refresh or if the client goes back to quick-quote.php page an e-mail, is sent.

How can I make it that if the client goes back or clicks refresh the client does not get another  e-mail.

if(isset($_POST['submit'])) $email=$_POST['email'];
//echo "$email </br>";
//echo "$firstname </br>";
//echo "$lastname </br>";


$to      = '';
$subject = ' ';
$message = "The estimated  $business_name 
$headers = 'From: ' . "\r\n" .
    'Reply-To: ' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

<input type="text" name="business_name" />
  First Name:<br />
  <input type="text" name="firstname" />
  <br />
  Last Name:<br />
  <input type="text" name="lastname" />
<p>E-mail Address:<br />
<input type="text" name="email" size="40" />
<label for="label"></label>
  <label for="business_name"></label>
  <br />
<p><input name="submit" type="hidden" value="submit" />
  <input type="submit" name="submit" />



>The estimated Rate for <?php echo "$business_name"; ?> </p>

Open in new window

Question by:tomjenkins12
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26293090
Is this method called at the onload event of the HTML?
Or is it called from the script tag outside any function?


Author Comment

ID: 26293115
Onload event

LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26293158
If it at the onload event then these issues will happen.

You might want to submit on some other event.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Accepted Solution

ludofulop earned 2000 total points
ID: 26293255
after sending the email, redirect user to a thank  you page:

header('Location: index.php?act=thankyou');

or, use session:

if (!$_SESSION['mailsent'])
  $_SESSION['mailsent'] = 1;

don't forget to reset somewhere the session value (user to be able to send another email).

i recommend you to use the first solutin (redirect)
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26293287
@ludofulop: Will it prevent the email from getting sent in case user comes back to the same page?
LVL 15

Expert Comment

ID: 26293390
@gurvinder372: no, you are right... but mostly i'm doing it this way, and have no problems (or maybe user lamas are avoiding my sites)

but OK, seems like session is better solution
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26293610
The usual way to do this is either to

1) Use two scripts - one that does the sending of the emails and then on completion uses the header() function to redirect to a simple "thank you" page which does nothing other than display a "Thank you" message. They can press Reload until they are blue in the face and it will not resend

2) (a bit like the session thing above) Use the same page but make its default behaviour to display a "thank you" unless some parameters are passed to cause an email send. When the send is complete use the header() function to load the page with no parameters.



Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question