通讯确认/订阅确认

时间:2013-03-19 03:03:18

标签: php validation newsletter

我的网站上有一份简报订阅表格,我希望它转到

页面

确认用户已注册。目前它只是刷新页面并给出

你在表单框中有一条消息。测试后,它也不关心是否

你错误地把电子邮件地址等等。我想它所以它检查这个信息只有

提交正确的电子邮件地址。目前的代码是

<?php

// get vars
$email_address = strtolower(trim($_REQUEST['email_address']));

if((strlen($email_address) > 0) && (strpos($email_address, "@")))
{
    // add to db
    $newsletterQry = db_query("SELECT * FROM newsletter_subscribers WHERE email='" . mysql_real_escape_string($email_address) . "'");
    if(db_num_rows($newsletterQry) == 0)
    {
        // add
        db_query("INSERT INTO newsletter_subscribers (email, created) VALUES ('" . mysql_real_escape_string($email_address) . "', NOW())");
    }
}

// return back to the index page with confirmation
header("location: ".$_SERVER["HTTP_REFERER"]."?nlMsg=".urlencode("You've been added to the site newsletter."));
exit;
?>

1 个答案:

答案 0 :(得分:0)

请参阅How to validate an Email in PHP上的解决方案。 您希望使用RegEx或PHP的内置filter_var()函数来清理电子邮件地址。

<?php
$email_address = strtolower(trim($_REQUEST['email_address']));
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    // This email address is valid insert into newsletter 
}