【发布时间】: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。 -
最好总是使用准备好的语句。
-
我会调查准备好的陈述。我以前没有使用过这种技术。