[英]Google reCaptcha v2 (Checkbox) Verification
我有联系表格,它工作正常。 我已经将它与Google ReCaptcha v2集成在一起,并且显示在我的联系表单下方,但是无论用户是否选择了reCaptcha,表单都将提交。 我想验证是否只有用户检查了Google ReCaptcha才提交表单,否则它会向用户显示一条消息以进行检查。
这是我的HTML表单(contact.html):
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer>
</script>
</head>
<body>
<form action="contact_us.php" method="post">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your
Name *" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email *" required>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Contact Number">
</div>
<div class="form-group">
<textarea class="form-control textarea" name="message" placeholder="Message"></textarea>
</div>
<div class="form-group">
<div class="g-recaptcha" data sitekey="have_enter_my_site_key_here"></div>
</div>
<div class="form-group">
<input type="submit" name="submit" class="form-control btn-
submit" Value="Send">
</div>
</form>
</body>
</html>
这是PHP代码(contact_us.php):
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from ='';
$email_subject = "Contact Form";
$email_body = "User Name: $name \n".
"User Email: $email \n".
"Phone Number: $phone \n".
"User Message: \n $message \n";
$to = "my_Email_here";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = mail($to,$email_subject,$email_body,$headers);
if($result) {
echo "Sent Successfully";
}
else{
echo "Something Went wrong. Please try again";
}
?>
帮助我如何使用我的HTML表单验证google ReCaptcha。
<?php
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
// Your site secret key obtained from Google
$secret = '#####################################';
$grResponse = $_POST['g-recaptcha-response'];
// Verify with Google Servers
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $grResponse);
$responseData = json_decode($verifyResponse);
if ($responseData->success) {
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = '';
$email_subject = "Contact Form";
$email_body = "User Name: $name \n" . "User Email: $email \n" . "Phone Number: $phone \n" . "User Message: \n $message \n";
$to = "my_Email_here";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = mail($to, $email_subject, $email_body, $headers);
if ($result) {
echo "Sent Successfully";
}
else {
echo "Something Went wrong. Please try again";
}
} else {
echo "################################";
}
} else {
echo "################################";
}
?>
请检查以下代码:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.