因此,我遇到了自定义PHP联系人表格阻止发送垃圾邮件的问题。我已经添加了Google ReCaptcha,还添加了一个功能来检查是否已填写隐藏字段,然后不发送消息。但是我的客户仍在接收垃圾邮件。客户端最近还从HubSpot迁移了代码,想知道我缺少的小部件中是否内置了某些内容。我是PHP新手,所以请原谅任何菜鸟错误:)。预先感谢您的帮助!
HTML表单:
<div class="contact-form">
<form id="contact-form" method="post" action="contact-form-handler.php">
<input name="companyname" type="text" id="companyName" placeholder=" Company Name" required>
<input name="name" type="text" id="contactName" placeholder=" Contact Person" required>
<input name="email" type="email" id="Email" placeholder=" Your Email" required>
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
<input type="tel" id="Phone" name="Phone" placeholder=" Phone Number" required>
<textarea name="message" class='form-control' placeholder=" Write your message here..." style="white-space:pre-wrap; height:200px;width:500px;" row="4" required></textarea>
<div class="g-recaptcha" data-sitekey="6LcqLWkUA2AAADEMnsD4sZEj4BqmqGhx8CN5Hhqf" data-callback="recaptcha_callback"></div>
<input type="submit" id="submit_btn" name="submit_form" value="SEND MESSAGE" onclick="myFunction()" disabled>
</form>
</div>
PHP处理程序
if (isset($_POST['submit_form'])) {
$name = $_POST['name'];
$secretKey = "6LcqLWkUAAAAAOG_Z9lpScLz0nftfFoYgpENfwDp";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response->success)
echo "Verification success. Your name is $name";
else
echo "Verification Failed";
}
$public_key = "6LcpmGgUAAAAAI6O2SQv1TdYu9z9yzmXclU2-rzu";
$private_key = "6LclmGgUAA2AALd9pZTaOzOV4tThdZNLeJ56WNno";
$reCaptchaUrl = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
$companyname = $_POST['companyname'];
$name = $_POST['name'];
$url = $_POST['url'];
$email = $_POST['email'];
$phone = $_POST['Phone'];
$message = $_POST['message'];
/* Check if the form has been submitted */
if(array_key_exists('submit_form',$_POST))
{
/* The response given by the form being submitted */
$response_key = $_POST['g-recaptcha-response'];
/* Send the data to the API for a response */
$response = file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDR']);
/* json decode the response to an object */
$response = json_decode($response);
/* if success */
if($response->success == 1)
{
echo "You passed validation!";
}
else
{
echo "You are a robot and we don't like robots.";
}
// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == ''){
// then send the form to your email
mail( 'danmeier513@gmail.com', 'danielstevenmeier@gmail.com', 'Contact Form', print_r($_POST,true) );
}
// otherwise, let the spammer think that they got their message through
}
答案 0 :(得分:6)
嗯,简单检查一下:您有一行内容如下:
echo "You are a robot and we don't like robots.";
...在该行之后,无论ReCaptcha检查如何,您都可以发送邮件。
如果验证码检查失败,则应立即通过exit
或die
之类的脚本停止脚本。这可能是第一步-可能,如果仍然有垃圾邮件传递给客户,则应该在代码中添加更多日志记录以进一步调试。