如何防止在PHP中多次发送邮件?

时间:2015-11-16 13:02:24

标签: javascript php html forms email

我已经制作了一个小表格,完成后会发送邮件并保存cookie。现在,每次用户刷新该页面时,都会发送邮件。我该如何防止这种情况?

如何使邮件仅在首次提交时发送?

以下是代码:

<?php

//Check for errors and display them 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$firstname=isset($_GET['firstname']) ? $_GET['firstname'] : ''; // Auto-populate field
$lastname=isset($_GET['lastname']) ? $_GET['lastname'] : ''; // Auto-populate field
$compname=isset($_GET['companyname']) ? $_GET['companyname'] : ''; // Auto-populate field
$email=isset($_GET['email']) ? $_GET['email'] : '';  // Auto-populate field
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
if($firstname !=''&& $lastname !=''&& $company !='')
{
//  To redirect user to accepted page
    //header("Location:accepted.php");
            die();
}
}
//  To redirect user to declined page
//if (isset($_POST['notsubmit'])) {
    //header("Location:declined.php");
//}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="noindex, nofollow" />
<link rel="icon" href="images/logo.ico" type="image/ico">
<title>Sign-Up Form</title>
</head>
<body>
 <form class="conf-form" method="POST" action="accepted.php">
    <legend>Please input your information:</legend><br />
    First name:<br>
    <input class="fst-name" type="text" name="firstname" value="<?=$firstname?>">
    <br><br />
    Last name:<br>
    <input class="lst-name" type="text" name="lastname" value="<?=$lastname?>">
    <br /><br />
    Position title:<br />
    <input class="pos-title" type="text" name="positiontitle" value="Position Title"/>
    <br /> <br />
    Company name:<br />
    <input class="cmp-name" type="text" name="companyname" value="<?=$compname?>">
    <br /><br />
     <textarea rows="4" cols="50" name="comment"></textarea>
     <br /><br />
    <input id="save-me" type="submit" value="Save me a seat" name="submit">&nbsp;&nbsp;&nbsp;&nbsp;<input id="not-int" type="submit" value="Not interested, thank you" name="notsubmit">
    <input class="email" type="text" name="email" value="<?=$email?>" style="display: none" />
</form> 
</body>
</html>

accepted.php:

<?php
//Check for errors and display them 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

/*----------------------------------------------------*/
//START GRABBING INFORMATION FROM URL LINK

if(isset($_POST['submit'])) {

    $_SESSION['firstname']=$_POST['firstname'];
    $_SESSION['lastname']=$_POST['lastname'];
    $_SESSION['companyname']=$_POST['companyname'];
    $_SESSION['email']=$_POST['email']; 
    $_SESSION['comment']=$_POST['comment']; 
    setcookie("invitation", 'accepted', time()+60*60*24*205, "/", "www.example.com");
    $to      = 'marketing@web.com';
    $subject = 'Invitation Accepted From - '.$_SESSION['companyname']." - ".$_SESSION['email'].'';
    $message = 'You have a invitation acceptance request from '.$_SESSION['firstname'].' '.$_SESSION['lastname'].' - '.$_SESSION['companyname']. "\r\n". "\r\n".
    'First Name: '.$_SESSION['firstname']. "\r\n".
    'Last Name: '.$_SESSION['lastname']. "\r\n".
    'Company Name: '.$_SESSION['companyname']. "\r\n".
    'Email : '.$_SESSION['email']. "\r\n".
    'Comment: '.$_SESSION['comment']. "\r\n". "\r\n".
    'Please record this in the master delegates sheet!'. "\r\n". "\r\n".
    'Kind Regards,'. "\r\n". "\r\n".
    'Marketing' ;
    $headers = 'From: web@mail.com' . "\r\n" .
        'Reply-To: web@mail.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
}
else
{
    header("Location:declined.php");
}

?>

2 个答案:

答案 0 :(得分:0)

您可以使用GET方法而不是POST,因为POST重新提交表单。这就是你的电子邮件被重新发送的原因。

答案 1 :(得分:0)

我想出了这个,所以我发帖只是为了与社区分享。这很有效:

if(!isset($_COOKIE['mailsent'])) {
   // send mail!
   setcookie('mailsent');
}