我有一个简单的php邮件脚本,它有一个客户端验证(基于jquery),工作正常。但有时有人会直接发帖,我会重新发送许多空的垃圾邮件。
如果某个字段不为空(空),我如何检查。例如,“usermail”变量。
<?php
$sendto = "youremail@youremail.com";
$usermail = $_POST['email'];
$content = nl2br($_POST['msg']);
$subject = "New Feedback Message";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "</body></html>";
if(@mail($sendto, $subject, $msg, $headers)) {
echo "true";
} else {
echo "false";
}
?>
这不起作用:
<?php
if(!empty($usermail)) {
$sendto = "mymail@mail.com";
$usermail = $_POST['email'];
$content = nl2br($_POST['msg']);
$subject = "New Feedback Message";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "</body></html>";
if(@mail($sendto, $subject, $msg, $headers)) {
echo "true";
} else {
echo "false";
}
}
?>
答案 0 :(得分:0)
好吧,你可以使用函数empty()
if (empty($_POST['email']) || empty($_POST['msg'])) {
exit();
}
更好的方法是进行正确的电子邮件验证和消息验证。例如:
@
是否存在。答案 1 :(得分:0)
使用!empty($usermail)
或$usermail !== '';
。您还应该确保页面也是$_POST['submitName']
。
答案 2 :(得分:0)
您可以使用以下语法检查变量的值。
if ($a==null)
{
exit(0)
}