Google 使用我的 jquery/ajax 函数重新验证
Google recaptcha with my jquery/ajax function
搜索有关设置 reCaptcha 的内容似乎会导致使用 jQuery 验证。但是我不确定如何在 ajax 表单提交的上下文中执行此操作。
目前我正在使用 Validity,而不是 jQuery 验证。我的 header 中有脚本参考和适当的密钥。
我应该在流程中的哪个位置验证 reCaptcha?
<form id="intake-form" class="grid-form" action="javascript:void(0);">
...
<div class="g-recaptcha" data-sitekey="key_here"></div>
<input type="submit" name="submit" value="Send!" />
<br />
<p id="formstatus"></p>
</form>
$("#intake-form").submit(function () {
var str = $(this).serialize();
if (validateIntakeForm()) { // validate intake fields with validity
$.ajax({
type: "POST",
url: global['base']+"intakeform",
data: str,
success: function (msg) {
$("#formstatus").ajaxComplete(function (event, request, settings) {
if (msg == 'success') {
result = '<div class="successmsg">Your request has been sent.';
$('#intake-form').clearForm();
} else {
result = 'There was a problem sending your message.<br />' + msg;
}
$(this).html(result);
});
}
});
return false;
}
});
我已经在我的一个网站上成功实施了 recaptcha。
我已经很久没有玩过它了,所以我不知道 Google 是如何处理这个问题的。
但无论如何,这是我的工作代码的简化版本,希望它能对你有所帮助。
<body>
<form method="post" action="">
<!-- your regular form fields here... -->
<div class="g-recaptcha" data-callback="processCaptcha" data-sitekey="THE_KEY_HERE"></div>
<!-- or your regular form fields here... -->
<input type="submit" value="Send"/>
</form>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=eng"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</body>
对于 src="https://www.google.com/recaptcha/api.js?hl=eng" 的脚本,
我注意到对此 link 的引用:https://developers.google.com/recaptcha/docs/language。
服务器端:
<?php
$secret = "THE_KEY_HERE";
$errors = [];
$gRecaptchaResponse = $_POST["g-recaptcha-response"];
$recaptcha = new \ReCaptcha\ReCaptcha($secret); // this class is provided by google
$resp = $recaptcha->verify($gRecaptchaResponse, $_SERVER['REMOTE_ADDR']);
if (false === $resp->isSuccess()) {
// $errors = $resp->getErrorCodes();
$errors[] = "Boo, you are a bot";
}
if (empty($errors)) {
// your success routine here
}
else {
// your error routine here
}
Google 将为您验证您的重新验证码。
检查下面的例子。
<?php
//first i retrieved the recaptcha coming through post method.
//i have used post method, you can use any method you want.
$recaptcha = $_POST['g-recaptcha-response'];
//After retrieving send a post curl request to verify it.
//you will have to send your secret key along with it.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"secret=*******************************=".$recaptcha);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
$server_output = json_decode($server_output,true);
curl_close ($ch);
if($server_output['success']){
// captacha validated successfully.
}else{
// invalid captcha
}
?>
你可以参考docs.
您可以使用 google 插件来验证 php 中的 recaptcha
试试这个 link
然后将此代码包含在您的 php 验证脚本中
$recaptcha = new \ReCaptcha\ReCaptcha( RECAPTCHA_SECRET_KEY );
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"] );
if(!$resp->isSuccess()) //ERROR CAPTCHA NOT VALIDATED
搜索有关设置 reCaptcha 的内容似乎会导致使用 jQuery 验证。但是我不确定如何在 ajax 表单提交的上下文中执行此操作。
目前我正在使用 Validity,而不是 jQuery 验证。我的 header 中有脚本参考和适当的密钥。
我应该在流程中的哪个位置验证 reCaptcha?
<form id="intake-form" class="grid-form" action="javascript:void(0);">
...
<div class="g-recaptcha" data-sitekey="key_here"></div>
<input type="submit" name="submit" value="Send!" />
<br />
<p id="formstatus"></p>
</form>
$("#intake-form").submit(function () {
var str = $(this).serialize();
if (validateIntakeForm()) { // validate intake fields with validity
$.ajax({
type: "POST",
url: global['base']+"intakeform",
data: str,
success: function (msg) {
$("#formstatus").ajaxComplete(function (event, request, settings) {
if (msg == 'success') {
result = '<div class="successmsg">Your request has been sent.';
$('#intake-form').clearForm();
} else {
result = 'There was a problem sending your message.<br />' + msg;
}
$(this).html(result);
});
}
});
return false;
}
});
我已经在我的一个网站上成功实施了 recaptcha。 我已经很久没有玩过它了,所以我不知道 Google 是如何处理这个问题的。
但无论如何,这是我的工作代码的简化版本,希望它能对你有所帮助。
<body>
<form method="post" action="">
<!-- your regular form fields here... -->
<div class="g-recaptcha" data-callback="processCaptcha" data-sitekey="THE_KEY_HERE"></div>
<!-- or your regular form fields here... -->
<input type="submit" value="Send"/>
</form>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=eng"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</body>
对于 src="https://www.google.com/recaptcha/api.js?hl=eng" 的脚本, 我注意到对此 link 的引用:https://developers.google.com/recaptcha/docs/language。
服务器端:
<?php
$secret = "THE_KEY_HERE";
$errors = [];
$gRecaptchaResponse = $_POST["g-recaptcha-response"];
$recaptcha = new \ReCaptcha\ReCaptcha($secret); // this class is provided by google
$resp = $recaptcha->verify($gRecaptchaResponse, $_SERVER['REMOTE_ADDR']);
if (false === $resp->isSuccess()) {
// $errors = $resp->getErrorCodes();
$errors[] = "Boo, you are a bot";
}
if (empty($errors)) {
// your success routine here
}
else {
// your error routine here
}
Google 将为您验证您的重新验证码。 检查下面的例子。
<?php
//first i retrieved the recaptcha coming through post method.
//i have used post method, you can use any method you want.
$recaptcha = $_POST['g-recaptcha-response'];
//After retrieving send a post curl request to verify it.
//you will have to send your secret key along with it.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"secret=*******************************=".$recaptcha);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
$server_output = json_decode($server_output,true);
curl_close ($ch);
if($server_output['success']){
// captacha validated successfully.
}else{
// invalid captcha
}
?>
你可以参考docs.
您可以使用 google 插件来验证 php 中的 recaptcha 试试这个 link 然后将此代码包含在您的 php 验证脚本中
$recaptcha = new \ReCaptcha\ReCaptcha( RECAPTCHA_SECRET_KEY );
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"] );
if(!$resp->isSuccess()) //ERROR CAPTCHA NOT VALIDATED