检查电子邮件验证和多封电子邮件的简单方法?

时间:2013-09-06 21:20:16

标签: php mysql email

我已经在这个网站和互联网上搜索了一个简单的解决方案,用于检查是否有两次电子邮件输入我的数据库以及是否是有效的电子邮件。我找不到简单的解决方案。这是我的表格和PHP。提前谢谢

    <div class="email-survey">  
                <?php if(isset($success)) { ?>
                    <div class="success_survey">Thanks you! Your survey has been submitted</div>
                <?php } ?>

                <?php if(isset($error)) { ?>
                    <div class="error_survey">
                <?php echo $error; ?>
                    </div>
                <?php } ?>

        <form name="settings" action="/survey-confirm.php" method="post">
        <input type="text" name="email" /> <br />
        <input type="submit" name="submit" value="submit" />
        </form>
   </div>

require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');

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

        $email = $_POST['email'];
        if(empty($_POST['email'])) { 

            $error = "Please enter a valid email";

        }else{
            $success = true;    

            mysql_query("INSERT INTO survey_email 
                (email) VALUES('".$_POST['email']."' ) ") 
                or die(mysql_error()); 
            header('Location: /survey-email-confirm.php');
        }
    }

?>

3 个答案:

答案 0 :(得分:1)

发送给用户的链接。当他们点击它时,使用$_GET信息来验证它是否是有效的电子邮件。当然,您应该使用MySQL SELECT查询来测试是否存在电子邮件以防止重复输入。以下显示mysqli面向对象的方法,我建议您不要重新输入内容:

include 'db.php';
if(isset($_POST['submit'])){
  $em = trim($_POST['email']);
  if($em === '' || !preg_match('/^.{1,62}@.{1,62}\.[a-zA-Z]{2,4}$/', $em)){
    $error = 'Please Enter a Valid Email';
  }
  else{
    $sml = $db->query("SELECT email FROM survey_mail WHERE email='$em'");
    if($sml->num_rows > 0){
      $error = 'Sorry, this Email is Already in Use';
    }
    else{
      $eml = $db->real_escape_string($em);
      $db->query("INSERT survey_email (email) VALUES ('$eml')");
      //the below leaves out the $doctype, beginning html tag and your $css
      $dt = date('l, F j, Y, g:i:sa T', time());
      $msg = "$doctype<title>Your Email Was Received</title><style type='text/css'>$css</style></head><body><a href='http://www.domain/wherever.php?em=someCrazyHashOrSomthing'>Please Click the Following Link to Confirm Your Email Address is Valid</a></div><div><b>Sent:</b> $dt <br /><b>Email:</b> $em <br /></div></body></html>";
      mail($em, 'Thank You for Contacting Us', $msg, "From: name@domain.com\r\ncontent-type: text/html");
    }
  }
  $sml->free(); $db->close();
}
else{
  $em = 'example@example.com';
}

当然,现在您需要PHP来验证电子邮件中的链接信息。我不打算为你做所有的工作,所以玩得开心。您可以考虑使用我的资源库PHPglue。它将对数据进行多次检查,只需很少的工作,并且包含自动JavaScript错误,而无需了解JavaScript。

答案 1 :(得分:1)

require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php');

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

    // get the email address
    $email = strip_tags(trim($_POST['email']));

    // see if email is empty or invalid
    if($email == '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error_message = 'Please enter a valid email address';
    } else {
        // email is valid
        $success = true;    

        // check if email address is already in database
        $sql = mysql_query("SELECT email FROM users WHERE email='".mysql_real_escape_string($email)."'") or die(mysql_error());
        $check = mysql_fetch_assoc($sql);

        if($check > 0) {
            // email address exists
            $error_message = 'That email address exists';
        } else {
            // email address does not exist
            mysql_query("INSERT INTO survey_email (email) VALUES('".mysql_real_escape_string($email)."' ) ") or die(mysql_error()); 
            header('Location: /survey-email-confirm.php');
        }
    }
}

你应该使用mysqli或PDO,因为mysql是折旧的。此外,永远不要将数据放入您的数据库而不首先清理它 - 即使您的PHP脚本创建它。永远不要信任数据,特别是来自用户输入。

编辑:我误读了OP的问题。更新了新答案。

答案 2 :(得分:1)

好的,所以我将描述你应该采取的步骤,以获得一个很好的解决方案:

  1. 在PHP的数据访问层中,您应该运行一个查询,检查数据库中是否有与此电子邮件一致的电子邮件地址。这样的事情应该在数据库服务器上运行(谨防SQL注入):

    SELECT EXISTS(SELECT FROM FROM users WHERE email ='test@example.com')

  2. 如果电子邮件存在则显示错误

  3. 实现一个PHP函数,它将使用正则表达式测试电子邮件地址是否有效。使用谷歌找到一个很好的正则表达式。

  4. 如果电子邮件的格式无效,则显示错误

  5. 使用您在PHP函数中使用的相同正则表达式,在Javascript中实现电子邮件格式验证程序(可选,但最好不要在电子邮件地址无效的情况下发送请求)。

    < / LI>
  6. 实施表单的提交事件,仅在电子邮件地址通过Javascript验证时才提交。

  7. 因此,当客户端使用表单时,您首先使用Javascript验证程序检查电子邮件地址,并仅在有效时发送请求,在PHP中检查电子邮件格式,如果有效则检查数据库中是否重复。处理可能的故障和成功方案。快乐的调试。