【发布时间】:2012-09-01 18:49:21
【问题描述】:
我是 stackoverflow 的新手,找不到我的问题的答案;如何在 php 中保护我的 mail() 代码以防止人们添加最终会导致大量邮件的密件抄送?当在我的网站中输入新评论时,我的网站使用 PHP mail() 服务向我发送电子邮件。防止人们篡改它的最佳方法是什么,例如删除密件抄送?到目前为止我所拥有的是:
function mres($input){
if (get_magic_quotes_gpc()){
$input = stripslashes($input);
}
return mysql_real_escape_string($input);
}
$name = strip_tags(mres($_POST['name']));
$comment = strip_tags(mres($_POST['comment']));
$to = 'myself@gmail.com';
$subject = 'Website - comment';
$body = 'A new comment has been entered on the website.'."\n\n"."$name".' said: '."\n\n"."$comment";
mail($to,$subject,$body);
【问题讨论】:
-
不重新发明轮子,开始使用phpmailer怎么样? PS:你为变量申请
mysql_real_escape_string做什么? -
该函数询问服务器魔术引号是默认打开还是关闭。根据结果,将激活适当的安全措施,从而在发表评论时防止 sql 注入或类似情况。
-
您不需要(也不应该)使用
mysql_real_escape_string,除非您立即将结果用作 SQL 查询的一部分。 (并且mail()不是 SQL 查询。)