Javascript 我需要对我的表单提交(电子邮件)进行哪些改进,以阻止我收到的垃圾邮件,并使我的表单更加安全?
每天早上10点53分,我都会收到1-2封空白邮件,这些邮件是由某种实体使用我的send.php文件发送的 我知道这是我的php发送文件在我的网站上的结果,但我没有太多的php知识。几周前,我花了很多时间寻找清晰的信息,告诉我如何使用php设置输入表单,使其最初发送并达到这一目的。但我知道我的代码中有一些非常明显的安全漏洞 我想知道我在我的表单上做了什么错事来锁定它,这样我就不会再被垃圾邮件击中了 我有一个javascript蜜罐,它只是一个输入字段,如果选择了隐藏字段,它会将一个值从true更改为false,表单将不再提交表单,而是会给用户一个警告,说他们已成功提交表单,页面只是重新加载,这可能不是一个很好的解决方案 我继续写,并用电子邮件替换了我的电子邮件some@email.comJavascript 我需要对我的表单提交(电子邮件)进行哪些改进,以阻止我收到的垃圾邮件,并使我的表单更加安全?,javascript,php,html,forms,Javascript,Php,Html,Forms,每天早上10点53分,我都会收到1-2封空白邮件,这些邮件是由某种实体使用我的send.php文件发送的 我知道这是我的php发送文件在我的网站上的结果,但我没有太多的php知识。几周前,我花了很多时间寻找清晰的信息,告诉我如何使用php设置输入表单,使其最初发送并达到这一目的。但我知道我的代码中有一些非常明显的安全漏洞 我想知道我在我的表单上做了什么错事来锁定它,这样我就不会再被垃圾邮件击中了 我有一个javascript蜜罐,它只是一个输入字段,如果选择了隐藏字段,它会将一个值从true更改
<?php
$sname = $_POST['name'];
$slastname = $_POST['last-name'];
$semail = $_POST['email'];
$semailconfirm = $_POST['confirm-email'];
$sphone = $_POST['phone-number'];
$smessage = filter_var($_POST['message'], FILTER_SANITIZE_STRING );
$formerrors = false;
if ($sname === '') :
echo "<div>Sorry, first name is a required field</div>";
endif; //input field empty
if (!(preg_match('/[a-zA-Z]+/', $sname)) ) :
echo "<div>Sorry, first name doesnt follow the allowed pattern</div>";
endif; //pattern doesnt match
if ($slastname === '') :
echo "<div>Sorry, last name is a required field</div>";
endif; //input field empty
if (!(preg_match('/[a-zA-Z]+/', $slastname)) ) :
echo "<div>Sorry, last name doesnt follow the allowed pattern</div>";
endif; //pattern doesnt match
if ($semail === '') :
echo "<div>Sorry, email is a required field</div>";
endif; //input field empty
if ($semailconfirm !== $semail) :
echo "<div>Sorry, emails must match</div>";
endif; //input field empty
if ($smessage === '') :
echo "<div>Sorry, your message has no content</div>";
endif; //input field empty
if (!($formerrors)) :
$to = "some@email.com";
$subject = "From $sname $slastname -- $semail -- $sphone";
$message = "$sname filled out the form";
$replyto = "From: $semail \r\n".
"Reply-To: some@email.com \r\n";
if (mail($to, $subject, $smessage)):
$msg = "Thanks for filling out my contact form";
else:
$msg = "problem sending message";
endif; //mail form data
endif; //check for errors
?>
我每天收到的电子邮件说
从--
文本正文中没有任何内容您从未将$formerrors设置为
您的其他检查只输出一个错误,但仍在处理表单
尝试:
出现错误时,您似乎忘记将$formerrors标志设置为true。尝试以下方法:
if (trim($sname) == '')
{
$formerrors = true;
echo "<div>Sorry, first name is a required field</div>";
}
Recaptcha是唯一经济有效的方法,所以我不会放弃。这很简单: 由于您使用的是php,以下是您需要添加php部分的表单:
<form method="post" action="verify.php">
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form>
然后,您可以在verify.php或任何提交表单数据的地方进行验证:
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
就这样
混淆电子邮件地址并实现ReCaptcha。您可以在这里下载并检查实现示例:别忘了爱尔兰语!应该允许像O'Brian这样的姓氏通过姓氏测试。还有已婚人士的连字号。这确实帮了大忙:谢谢你,我的表单是在一个普通的html上,将信息传递到一个php页面,我需要做一些调整才能让它工作吗?@Aron如果你喜欢答案,请投票给它可信度。没有什么需要调整的,只要在代码中提到的地方插入您的公钥和私钥即可。
<form method="post" action="verify.php">
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form>
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>