use*_*224 0 php spam-prevention form-submit recaptcha contact-form
因此,我遇到了阻止发送垃圾邮件的自定义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>
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
嗯,一个简单的检查:你有一行包含以下内容:
echo "You are a robot and we don't like robots.";
Run Code Online (Sandbox Code Playgroud)
...在该行之后,无论ReCaptcha检查如何,您都会发送邮件.
如果验证码检查失败,您应该立即停止脚本,例如exit或die.这可能是第一步 - 如果仍有垃圾邮件进入您的客户,您可能应该在代码中添加更多日志记录以进一步调试.