使用php的Google ReCAPTCHA for html表单
最近在我的网站上,垃圾邮件的情况已经失去了控制,我决定我真的必须对此做些什么。google reCAPTCHA似乎是将人与机器人区分开来的最佳方式 我在PHP方面没有太多经验,这使得我很难完成实现 我的表单的HTML端:使用php的Google ReCAPTCHA for html表单,php,html,recaptcha,Php,Html,Recaptcha,最近在我的网站上,垃圾邮件的情况已经失去了控制,我决定我真的必须对此做些什么。google reCAPTCHA似乎是将人与机器人区分开来的最佳方式 我在PHP方面没有太多经验,这使得我很难完成实现 我的表单的HTML端: <div class="col-lg-6 no-gap contact-form"> <header class="sec-heading"> <br> &l
<div class="col-lg-6 no-gap contact-form">
<header class="sec-heading">
<br>
<h2>Contact</h2>
<span class="subheading">SUBHEADING</span>
</header>
<form action="assets/contact-form/contact-form.php" method="POST" class="form-ajax wow fadeInUp" data-wow-duration="1s" data-wow-delay=".1s">
<!-- Name -->
<div class="form-group">
<input type="text" name="name" id="name-contact-1" class="form-control validate-locally" placeholder="Write your name">
<label for="name-contact-1">Name</label>
<span class="pull-right alert-error"></span>
</div>
<!-- Email -->
<div class="form-group">
<input type="email" name="email" id="email-contact-1" class="form-control validate-locally" placeholder="Your email">
<label for="email-contact-1">Email</label>
<span class="pull-right alert-error"></span>
</div>
<!-- Select -->
<div class="form-group">
<select class="form-control" id="select-form" name="select" id="select">
<option id="optiona">Option A</option>
<option id="optionb">Option B</option>
<option id="optionc">Option C</option>
<option id="optiond">Option D</option>
</select>
<label for="select-form">Your choice</label>
</div>
<!-- Select -->
<div class="form-group">
<select class="form-control" id="select-form" name="select1" id="select1">
<option value="" disabled selected>Choose.. </option>
<option id="option1">Option 1</option>
<option id="option2">Option 2</option>
<option id="option3">Option 3</option>
<option id="option4">Option 4</option>
</select>
<label for="select-form">Options</label>
</div>
<!-- Message -->
<div class="form-group">
<textarea class="form-control" name="message" id="message-contact-1" rows="5" placeholder="Your message"></textarea>
<label for="message-contact-1">Message</label>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="---SECRET CODE FOR reCAPTCHA---"></div>
</div>
<br>
<input type="submit" name="submit" class="btn pull-right" value="Send message">
<!-- Ajax Message -->
<div class="ajax-message col-md-12 no-gap"></div>
</form>
<?php
if(isset($_POST['email'])) {
$email_to = "my@email.com";
$email_subject = "Message recieved from form";
function died($error) {
echo '<div class="alert alert-danger alert-dismissible wow fadeInUp" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Oops! Something seems to be incorrect. Please check for errors:</strong><br>';
echo $error."<br />";
echo '</div>';
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('Oops! Something seems to be incorrect. Please check for errors. ');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$message = $_POST['message']; // required
$select = $_POST['select']; // required
$select1 = $_POST['select1']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The email adress you entered is invalid<br />';
}
if(strlen($name) < 2) {
$error_message .= 'The name you have entered seems to be invalid<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered seems to be invalid<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "A message was recieved: <br>\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "<br><b>Name:</b> ".clean_string($name)."\n";
$email_message .= "<br><b>Email:</b> ".clean_string($email_from)."\n";
$email_message .= "<br><b>Options1:</b> ".clean_string($select)."\n";
$email_message .= "<br><b>Options2:</b> ".clean_string($select1)."\n";
$email_message .= "<br><b>Message:</b> ".clean_string($message)."\n";
$headers = 'From: '.$email_from."\r\n".
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<div class="alert alert-success alert-dismissible wow fadeInUp" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Thank you! Your message has been sent!
</div>
<?php } ?>
接触
副标题
名称
电子邮件
方案A
方案B
备选案文C
方案D
你的选择
选择。。
选择1
选择2
选择3
选择4
选择权
消息
我表单的PHP端:
<div class="col-lg-6 no-gap contact-form">
<header class="sec-heading">
<br>
<h2>Contact</h2>
<span class="subheading">SUBHEADING</span>
</header>
<form action="assets/contact-form/contact-form.php" method="POST" class="form-ajax wow fadeInUp" data-wow-duration="1s" data-wow-delay=".1s">
<!-- Name -->
<div class="form-group">
<input type="text" name="name" id="name-contact-1" class="form-control validate-locally" placeholder="Write your name">
<label for="name-contact-1">Name</label>
<span class="pull-right alert-error"></span>
</div>
<!-- Email -->
<div class="form-group">
<input type="email" name="email" id="email-contact-1" class="form-control validate-locally" placeholder="Your email">
<label for="email-contact-1">Email</label>
<span class="pull-right alert-error"></span>
</div>
<!-- Select -->
<div class="form-group">
<select class="form-control" id="select-form" name="select" id="select">
<option id="optiona">Option A</option>
<option id="optionb">Option B</option>
<option id="optionc">Option C</option>
<option id="optiond">Option D</option>
</select>
<label for="select-form">Your choice</label>
</div>
<!-- Select -->
<div class="form-group">
<select class="form-control" id="select-form" name="select1" id="select1">
<option value="" disabled selected>Choose.. </option>
<option id="option1">Option 1</option>
<option id="option2">Option 2</option>
<option id="option3">Option 3</option>
<option id="option4">Option 4</option>
</select>
<label for="select-form">Options</label>
</div>
<!-- Message -->
<div class="form-group">
<textarea class="form-control" name="message" id="message-contact-1" rows="5" placeholder="Your message"></textarea>
<label for="message-contact-1">Message</label>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="---SECRET CODE FOR reCAPTCHA---"></div>
</div>
<br>
<input type="submit" name="submit" class="btn pull-right" value="Send message">
<!-- Ajax Message -->
<div class="ajax-message col-md-12 no-gap"></div>
</form>
<?php
if(isset($_POST['email'])) {
$email_to = "my@email.com";
$email_subject = "Message recieved from form";
function died($error) {
echo '<div class="alert alert-danger alert-dismissible wow fadeInUp" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Oops! Something seems to be incorrect. Please check for errors:</strong><br>';
echo $error."<br />";
echo '</div>';
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('Oops! Something seems to be incorrect. Please check for errors. ');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$message = $_POST['message']; // required
$select = $_POST['select']; // required
$select1 = $_POST['select1']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The email adress you entered is invalid<br />';
}
if(strlen($name) < 2) {
$error_message .= 'The name you have entered seems to be invalid<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered seems to be invalid<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "A message was recieved: <br>\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "<br><b>Name:</b> ".clean_string($name)."\n";
$email_message .= "<br><b>Email:</b> ".clean_string($email_from)."\n";
$email_message .= "<br><b>Options1:</b> ".clean_string($select)."\n";
$email_message .= "<br><b>Options2:</b> ".clean_string($select1)."\n";
$email_message .= "<br><b>Message:</b> ".clean_string($message)."\n";
$headers = 'From: '.$email_from."\r\n".
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<div class="alert alert-success alert-dismissible wow fadeInUp" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Thank you! Your message has been sent!
</div>
<?php } ?>
&时代;
非常感谢。您的信息已发送!
虽然我是新手,但我一直在尝试按照教程进行操作,因此我尝试在php文件的顶部实现一个函数,如下所示:
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
'secret' => 'SECRET CODE FROM GOOGLE',
'response' => $_POST['g-recaptcha-response'],
],
]);
$response = json_decode(curl_exec($curl));
if (!$response->success) {
最肮脏的方法是用che-recaptcha检查代码包装实际的电子邮件发送代码。首先,将died()函数移到if{..}语句之外。您可以将该函数重用为一个非常简单的错误处理程序
<?php
//recaptcha test
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_POSTFIELDS => [
'secret' => 'SECRET CODE FROM GOOGLE',
'response' => $_POST['g-recaptcha-response'],
],
]);
$response = json_decode(curl_exec($curl));
//UPDATE: if your check pass, go on
if ($response->success) {
//your actual code.
} else {
died("Recaptcha missing.");
}
?>
感谢您抽出时间解决我的问题。当我尝试这种方法时,由于某种原因,没有任何东西可以阻止我在不选中recaptcha框的情况下发送表单。因此,尽管添加了代码和if{}语句,表单基本上还是可以工作的,就像没有recaptcha一样。我真的想不起来了。@EricL代码更新了。我刚刚使用了$response success
属性,与前面的代码中一样