如果验证码未成功解析,如何防止发送数据
how to prevent sending data if captcha is not resolved successfully
我有一个输入和一个 google 重新验证
并且只有在验证码被解决时才需要发送输入值
页面重新加载后 grecaptcha.getResponse()
的值为空字符串
当通过单击解析验证码时 - 该值是一个长字符串 - 每次加载页面后都会有所不同
如果验证码说 - you're a robot
或类似的东西
如何知道值是多少
换句话说,如果验证码没有成功解决,如何取消发送数据?
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha" data-sitekey="6Lf-wAIaAAAAALa36iP4tIvwCeub0-bAB3QfqWbP"></div>
我找到的最好的是这里 - https://developers.google.com/recaptcha/docs/verify
并尝试了这个:
$('#btn').on('click', function(){
let rc = grecaptcha.getResponse();
console.log(rc.success); // undefined
});
任何帮助
这里是 Php
中的一个工作示例
define('SITE_KEY','SDFSDF4UAAAAAM-ISDM2lM5WESDVSDCERGDFGSDFG');
define('SECRET_KEY','SGSFGFDSAACJ_OxaXzSayvQS_ABCDECSDFSDF');
if ($_POST) {
function getcaptcha($secretkey){
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?
secret=".SECRET_KEY."&response={$secretkey}");
$respuesta = json_decode($response);
return $respuesta;
}
$return = getcaptcha($_POST['g-recaptcha-response']);
var_dump($return);
if ($return->success == true && $return->score >0.5) {
echo "You are a Person...";
}
else{
echo "You are a robot... ";
}
}
和Javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recaptcha V3 by AleDC</title>
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY; ?>"></script>
<form action="index.php" method="post">
Nombre: <input type="text"> <br>
TOKEN: <input type="text" name="g-recaptcha-response" id="g-recaptcha-response"><br>
<input type="submit" value="submit">
</form>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo SITE_KEY; ?>', {action: 'homepage'})
.then(function(token) {
console.log(token);
document.getElementById("g-recaptcha-response").value=token;
});
});
</script>
请记住,您必须在 google recaptcha 门户中注册您的网站
我有一个输入和一个 google 重新验证
并且只有在验证码被解决时才需要发送输入值
页面重新加载后 grecaptcha.getResponse()
的值为空字符串
当通过单击解析验证码时 - 该值是一个长字符串 - 每次加载页面后都会有所不同
如果验证码说 - you're a robot
或类似的东西
如何知道值是多少
换句话说,如果验证码没有成功解决,如何取消发送数据?
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha" data-sitekey="6Lf-wAIaAAAAALa36iP4tIvwCeub0-bAB3QfqWbP"></div>
我找到的最好的是这里 - https://developers.google.com/recaptcha/docs/verify
并尝试了这个:
$('#btn').on('click', function(){
let rc = grecaptcha.getResponse();
console.log(rc.success); // undefined
});
任何帮助
这里是 Php
中的一个工作示例 define('SITE_KEY','SDFSDF4UAAAAAM-ISDM2lM5WESDVSDCERGDFGSDFG');
define('SECRET_KEY','SGSFGFDSAACJ_OxaXzSayvQS_ABCDECSDFSDF');
if ($_POST) {
function getcaptcha($secretkey){
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?
secret=".SECRET_KEY."&response={$secretkey}");
$respuesta = json_decode($response);
return $respuesta;
}
$return = getcaptcha($_POST['g-recaptcha-response']);
var_dump($return);
if ($return->success == true && $return->score >0.5) {
echo "You are a Person...";
}
else{
echo "You are a robot... ";
}
}
和Javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recaptcha V3 by AleDC</title>
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY; ?>"></script>
<form action="index.php" method="post">
Nombre: <input type="text"> <br>
TOKEN: <input type="text" name="g-recaptcha-response" id="g-recaptcha-response"><br>
<input type="submit" value="submit">
</form>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo SITE_KEY; ?>', {action: 'homepage'})
.then(function(token) {
console.log(token);
document.getElementById("g-recaptcha-response").value=token;
});
});
</script>
请记住,您必须在 google recaptcha 门户中注册您的网站