大佬教程收集整理的这篇文章主要介绍了使用AJAX的ReCaptcha 2.0,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,这很愚蠢。
我做错了几件事:
if (status == "ok")
永远无法正常工作。我确实收到了电子邮件,但没有得到任何确认,现在我明白了为什么。感谢@Samurai帮助我弄清楚了事情。
<?php
// assemble the message from the POST fIElds
// getTing the captcha
$captcha = "";
if (isset($_POST["g-recaptcha-response"]))
$captcha = $_POST["g-recaptcha-response"];
if (!$captcha)
echo "not ok";
// handling the captcha and checking if it's ok
$secret = "MY_SECRET";
$response = Json_decode(file_get_contents("https://www.Google.com/recaptcha/API/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"]), truE);
// if the captcha is cleared with Google, send the mail and echo ok.
if ($response["success"] != falsE) {
// send the actual mail
@mail($email_to, $subject, $finalMsg);
// the echo goes BACk to the AJAX, so the user can kNow if everything is ok
echo "ok";
} else {
echo "not ok";
}
?>
我设法在我的网站上运行ReCaptcha 2.0。但是,只有当我不使用AJAX并让表单“自然”提交时,它才起作用。
我要提交带有验证码的表单,并在 不刷新页面的情况下 向用户发送成功提示,以提醒用户。
我尝试了以下代码,但似乎服务器未获得用户响应:
HTML:
<form class="form" action="javascript:void(0)" novalidate>
<!-- all the inputs... -->
<!-- captcha -->
<div class="input-group">
<div class="g-recaptcha" data-sitekey="6LdopgYTAAAAAE3ltWQGar80KUavaR-JblgPZjDI"></div>
</div>
<div class="errors" id="errors" style="display: none"></div>
<div class="input-group">
<input type="button" value="Send" class="btn-default right" id="submit">
<div class="clear"></div>
</div>
</form>
JS:
$('#submit').click(function(E) {
console.log('clicked submit'); // --> works
var $errors = $('#errors'),$status = $('#status'),name = $('#name').val().replace(/<|>/g,""),// prevent xss
email = $('#email').val().replace(/<|>/g,msg = $('#message').val().replace(/<|>/g,"");
if (name == '' || email == '' || msg == '') {
valid = false;
errors = "All fields are required.";
}
// pretty sure the problem is here
console.log('captcha response: ' + grecaptcha.getResponse()); // --> captcha response:
if (!errors) {
// hide the errors
$errors.slideUp();
// ajax to the php file to send the mail
$.ajax({
type: "POST",url: "http://orenurBACh.com/assets/sendmail.php",data: "email=" + email + "&name=" + name + "&msg=" + msg + "&g-recaptcha-response=" + grecaptcha.getResponse()
}).done(function(status) {
if (status == "ok") {
// slide down the "ok" message to the user
$status.text('Thanks! Your message has been sent,and I will contact you soon.');
$status.slideDown();
// clear the form fields
$('#name').val('');
$('#email').val('');
$('#message').val('');
}
});
} else {
$errors.text(errors);
$errors.slideDown();
}
});
php:
<?php
// assemble the message from the POST fields
// getTing the captcha
$captcha = '';
if (isset($_POST['g-recaptcha-response']))
$captcha = $_POST['g-recaptcha-response'];
echo 'captcha: '.$captcha;
if (!$captcha)
echo 'The captcha has not been checked.';
// handling the captcha and checking if it's ok
$secret = 'MY_SECRET';
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']),truE);
var_dump($responsE);
// if the captcha is cleared with google,send the mail and echo ok.
if ($response['success'] != falsE) {
// send the actual mail
@mail($email_to,$subject,$finalMsg);
// the echo goes BACk to the ajax,so the user can know if everything is ok
echo 'ok';
} else {
echo 'not ok';
}
?>
*php页面中 *的结果 :
captcha: The captcha has not been checked.array(2) { ["success"]=> bool(false) ["error-codes"]=> array(1) { [0]=> String(22) "missing-input-response" } } not ok
最重要的 是,如何在不自动处理其余POST数据的情况下手动获得输入响应?
以上是大佬教程为你收集整理的使用AJAX的ReCaptcha 2.0全部内容,希望文章能够帮你解决使用AJAX的ReCaptcha 2.0所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。