防止 php 网络联系表单垃圾邮件
我是一个业余的网页设计师,我在 stackoverflow.com 和其他网站上搜索过,并找到了许多针对我遇到的这个问题的修复程序,但是它们都没有工作(可能是因为我错误地实现了它们)。我希望有更多知识的人可以帮助我进行简单的修复,或者向我展示如何实现我发现的修复程序之一。
问题:我在我的企业网站上有一个非常简单的php联系表格。多年来,它一直运行良好,但在上周已被黑客入侵。我现在每天收到数百份联系表单提交,没有评论,它们只有(显然有效的)电子邮件地址,以及名称字段中的一串字符(如“58ee8b52eef46”)。
我已经尝试了几种技术来防止这种垃圾邮件,它们要么破坏了我的php表单,要么没有阻止垃圾邮件。如果可能的话,我想要一个不需要验证码扭曲文本测试的解决方案,并且不需要填写表单的所有字段。
这是我的完整PHP代码:
<?php
if(isset($_POST['email'])) {
$email_to = "myemail@example.com";
$email_subject = "website form submission";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if (!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$error_message = "";
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$headers = 'From: '.$email_from."\r\n" .
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you soon. You will now be redirected back to example.com.
<META http-equiv="refresh" content="2;URL=http://www.example.com">
<?php
}
die();
?>