【问题标题】:FILTER_VALIDATE_EMAILFILTER_VALIDATE_EMAIL
【发布时间】:2012-03-12 23:18:55
【问题描述】:

我知道之前已经讨论过这个问题,但是自从 2010 年末的这篇帖子和其他围绕那个时候提出问题的讨论 - Does FILTER_VALIDATE_EMAIL make a string safe for insertion in database? - 我已经尝试了一些描述的情况,例如使用单引号和 ` 字符在我使用 FILTER_VALIDATE_EMAIL 的电子邮件表单中,它已阻止将它们输入数据库。

最近的 PHP 版本是否修复了早期的问题,是否安全?

我也很想用mysql_real_escape_string(),想必这两个函数可以并行使用没有任何冲突?

这是我用来将地址放入数据库的邮件列表代码

    <?php
// connects the database access information this file
include("mailing_list_include.php");

// the following code relates to mailing list signups only
if (($_POST) && ($_POST["action"] == "unsub")) {
// trying to ubsubscribe; validate email addresses
if ($_POST["email"] == "") {
    header("Location: mailing_list_remove.php");
    exit;

} else {
    // connect to database
    doDB();

    // filtering out anything that isn't an email address
    if ( filter_var(($_POST["email"]), FILTER_VALIDATE_EMAIL)  == TRUE) {
        echo '';
    } else {
        echo 'Invalid Email Address';
        exit;
    }

    // check that email is in the database
    emailChecker($_POST["email"]);

    // get number of results and do action
    if (mysqli_num_rows($check_res) < 1) {
        // free result
        mysqli_free_result($check_res);

        // print failure message
        $display_block = "We couldn't find ".$_POST["email"].". No action has therefore been taken.";

    } else {
        // get value of ID from result
        while ($row = mysqli_fetch_array($check_res)) {
            $id = $row["id"];
        }

        // unsubscribe the address
        $del_sql =  "DELETE FROM subscribers
                    WHERE id = '".$id."'";
        $del_res =  mysqli_query($mysqli, $del_sql)
                    or die(mysql_error($mysqli));
        $display_block = " Your email address, ".$_POST["email"].", is unsubscribed!";
    }
    mysqli_close($mysqli);
}
}
?>
<html>
<?php echo "$display_block";?>
</html>

【问题讨论】:

  • 您应该始终对要进入数据库的任何字符串数据使用mysql_real_escape_string
  • 最好总是使用准备好的语句。
  • 我会调查准备好的陈述。我以前没有使用过这种技术。

标签: php forms email


【解决方案1】:

filter_var 标志 FILTER_VALIDATE_EMAIL 将按照它所说的执行 = 将值验证为电子邮件,这意味着如果它不是电子邮件,它将返回 false。

您可能正在寻找FILTER_SANITIZE_EMAIL,它将(删除所有字符,除了字母、数字和!#$%&'*+-/=?^_`{|}~@.[])

FILTER_SANITIZE_STRING 将剥离标签,可选择剥离或编码特殊字符。

虽然我不推荐 w3schools,但它有一个 filter_var 标志列表 http://www.w3schools.com/php/php_ref_filter.asp

正如其他人所说,使用 PDO 准备好的查询是安全的,你可以在这里找到一个很好的 pdo 示例:http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#10 这将解释一些事情,这里还有一个简单的 pdo CRUD(创建检索更新删除)类:http://www.phpro.org/classes/PDO-CRUD.html

祝你好运……

【讨论】:

  • 这是一个邮件列表,其中有一个用于输入电子邮件地址的字段。因为我刚刚开始使用 PHP,所以很多这些东西对我来说都是新的。在这种情况下,您将如何使用准备好的查询?
猜你喜欢
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 2012-09-18
  • 2013-10-13
  • 2011-04-12
  • 2011-03-25
  • 1970-01-01
相关资源
最近更新 更多