我在实施recaptcha v2 chekbox时遇到了麻烦。我首先以表单形式实现,它仅在本地服务器上有效。经过多次尝试失败后,我搜索了一些代码,并在教程中原始使用它,并不断出错。 网址:https://afrumex.com/google_reCAPTCHA/prueba.html 希望有人可以帮忙。谢谢!
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ajax Contact Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<!-- ajax contact form -->
<section style="margin-top: 50px;">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<h5 class="card-header">Ajax Contact Form</h5>
<div class="card-body">
<form class="contact__form" method="post" action="mail.php">
<!-- form message -->
<div class="row">
<div class="col-12">
<div class="contact__msg" style="display: none">
<p>Your message was sent successfully.</p>
</div>
</div>
</div>
<!-- end message -->
<!-- form element -->
<div class="row">
<div class="col-md-6 form-group">
<input name="name" type="text" class="form-control" placeholder="Name" required>
</div>
<div class="col-md-6 form-group">
<input name="email" type="email" class="form-control" placeholder="Email" required>
</div>
<div class="col-md-6 form-group">
<input name="phone" type="text" class="form-control" placeholder="Phone" required>
</div>
<div class="col-md-6 form-group">
<input name="subject" type="text" class="form-control" placeholder="Subject" required>
</div>
<div class="col-12 form-group">
<textarea name="message" class="form-control" rows="3" placeholder="Message" required></textarea>
</div>
<div class="col-12 form-group">
<div class="g-recaptcha" data-sitekey="my site key here"></div>
</div>
<div class="col-12">
<input name="submit" type="submit" class="btn btn-success" value="Send Message">
</div>
</div>
<!-- end form element -->
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="main.js"></script>
</body>
</html>
PHP
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// access
$secretKey = 'my secret key here';
$captcha = $_POST['g-recaptcha-response'];
if(!$captcha){
echo '<p class="alert alert-warning">Please check the the captcha form1.</p>';
exit;
}
# FIX: Replace this email with recipient email
$mail_to = "demo@gmail.com";
# Sender Data
$subject = trim($_POST["subject"]);
$name = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["name"])));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$phone = trim($_POST["phone"]);
$message = trim($_POST["message"]);
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($phone) OR empty($subject) OR empty($message)) {
# Set a 400 (bad request) response code and exit.
http_response_code(400);
echo '<p class="alert alert-warning">Please complete the form and try again.</p>';
exit;
}
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$captcha&remoteip=$ip");
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo '<p class="alert alert-warning">Please check the the captcha form2.</p>';
} else {
# Mail Content
$content = "Name: $name\n";
$content .= "Email: $email\n\n";
$content .= "Phone: $phone\n";
$content .= "Message:\n$message\n";
# email headers.
$headers = "From: $name <$email>";
# Send the email.
$success = mail($mail_to, $subject, $content, $headers);
if ($success) {
# Set a 200 (okay) response code.
http_response_code(200);
echo '<p class="alert alert-success">Thank You! Your message has been sent.</p>';
} else {
# Set a 500 (internal server error) response code.
http_response_code(500);
echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send your message.</p>';
}
}
} else {
# Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo '<p class="alert alert-warning">There was a problem with your submission, please try again.</p>';
}
?>
答案 0 :(得分:0)
在尝试以我的形式实现复选框Recaptcha v2一段时间后,我尝试实现不可见的Recaptcha v2,并且此版本运行良好。我不知道这怎么可能!您可以在函数here中看到。
我不知道这与我的ssl证书有关(通过加密)。
这tutorial对我有很大帮助。
感谢您的评论ehacinom!
答案 1 :(得分:0)
我遇到了完全相同的问题,并将解决方案发布在INDIRECT下。
我测试过的解决方案是:
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret_key = 'YOUR_SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$message = 'Google reCaptcha Test';
if(!empty($response))
{
$url = $google_url."?secret=".$secret_key."&response=".$response;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$curlData = curl_exec($curl);
curl_close($curl);
$res = json_decode($curlData, TRUE);
if($res['success'] == 'true')
$message = "Success!";
else
$message = "Enter captcha again!";
}