我想为我的项目实现Google ReCaptcha,并且我一直遵循https://bootstrapious.com/p/bootstrap-recaptcha中的教程,使用从this site获得的本地默认键进行测试。
我认为前面的一切都进行得很好,因为它可以让我单击验证码,如果不单击,则不会让我通过。这是我在教程之后添加的HTML代码:
<form id="form_register" method="post" role="form" name="form_register">
<!-- Some inputs -->
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" data-callback="verifyRecaptchaCallback" data-expired-callback="expiredRecaptchaCallback"></div>
<input class="form-control d-none" data-recaptcha="true" required data-error="Please complete the Captcha">
<div class="help-block with-errors"></div>
</div>
<button type="submit" class="mixpanel_link_registro btn btn-lg btn-primary btn-extra-mobile w-100"> Save </button>
</form>
这是JS代码:
<script src='https://www.google.com/recaptcha/api.js'></script>
<script src="validator.js"></script>
<script>
$(function () {
window.verifyRecaptchaCallback = function (response) {
$('input[data-recaptcha]').val(response).trigger('change')
}
window.expiredRecaptchaCallback = function () {
$('input[data-recaptcha]').val("").trigger('change')
}
$('#form_register').validator();
$('#form_register').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "recaptcha.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data) {
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#form_register').find('.messages').html(alertBox);
$('#form_register')[0].reset();
grecaptcha.reset();
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
return false;
}
})
});
</script>
但是,当通过ajax进入我的recaptcha.php文件时,它不会验证我的响应。我使用的PHP代码是这样的:
require('recaptcha-master/src/autoload.php');
// ReCaptch Secret
$recaptchaSecret = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe';
error_reporting(E_ALL & ~E_NOTICE);
try {
if (!empty($_POST)) {
// validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
// we validate the ReCaptcha field together with the user's IP address
$server = $_SERVER['REMOTE_ADDR'] . ":" . $_SERVER['SERVER_PORT'];
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $server);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
}
} catch (\Exception $e) {
$_SESSION["error"] = $e;
}
打印recaptcha
变量会得到以下输出:
__PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ReCaptcha\ReCaptcha [secret:ReCaptcha\ReCaptcha:private] => 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe [requestMethod:ReCaptcha\ReCaptcha:private] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ReCaptcha\RequestMethod\CurlPost [curl:ReCaptcha\RequestMethod\CurlPost:private] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ReCaptcha\RequestMethod\Curl ) ) )
我得到一个异常消息:Recaptcha尚未通过验证。 我想知道该怎么做才能验证我的验证码。
谢谢。