我想插入一个联系人形式的新版本(V3)的Recaptcha。
我已经寻找了不同的解决方案,但它们只显示了代码的一部分,它们是不完整的,或者我得到了一个错误,并且大多数找到的解决方案对于如此简单的事情都非常复杂,我不理解代码。
发布于 2019-01-14 23:04:29
我已经搜索了这个论坛和其他论坛,以便在我的表单中实现新版本的ReCaptcha (V3)。我需要知道如何:
我没有找到任何简单的解决方案,可以向我展示所有这些要点,或者对于那些只想在他们的网站上插入联系人表单的人来说,这太复杂了。
最后,以多个解决方案的一些代码部分为例,我使用了一个简单且可重用的代码,您只需在其中插入相应的键。
这就是了。
基本JS代码
<script src="https://www.google.com/recaptcha/api.js?render=your reCAPTCHA site key here"></script>
<script>
grecaptcha.ready(function() {
// do request for recaptcha token
// response is promise with passed token
grecaptcha.execute('your reCAPTCHA site key here', {action:'validate_captcha'})
.then(function(token) {
// add token value to form
document.getElementById('g-recaptcha-response').value = token;
});
});
</script>基本的HTML代码
<form id="form_id" method="post" action="your_action.php">
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
<input type="hidden" name="action" value="validate_captcha">
.... your fields
</form>基本的PHP代码
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
else
$captcha = false;
if(!$captcha){
//Do something with error
}
else{
$secret = 'Your secret key here';
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']));
if($response->{'success'}==false)
{
//Do something with error
}
}
//... The Captcha is valid you can continue with the rest of your code
//... Add code to filter access using $response . score
if ($response->{'success'}==true && $response->{'score'} <= 0.5) {
//Do something to denied access
}您必须使用$response->{'score'}的值来过滤访问。它的取值范围为0.0到1.0,其中1.0表示与站点的最佳用户交互,0.0表示最差的交互(如机器人)。您可以看到一些在ReCaptcha documentation中使用的示例。
你只需要添加你的密钥,不需要更多的改变:
src="https://www.google.com/recaptcha/api.js?render=your reCAPTCHA site key here"
grecaptcha.execute('your reCAPTCHA site key here'和
$secret = 'Your secret key here';显然,在本例中,您还必须更改表单的操作:
action = "your_action.php"发布于 2021-02-16 04:33:11
在上面的答案中,需要更新这些行,以便能够读取PHP中的响应值:
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']));
$response->{'success'}
$response->{'score'}发布于 2022-01-04 18:24:12
看起来谷歌从第一个答案开始改进了他们的文档。这就是我是怎么做的。
表单中的客户端集成:
这里有相关的文档:https://developers.google.com/recaptcha/docs/v3
根据Google的说法,您应该在每个页面上包含Recaptcha API,以便它可以观察用户的行为。因此,我在页脚的末尾添加了这一行,它包含在每个页面中(不需要参数):
<script src="https://www.google.com/recaptcha/api.js"></script>在表单上使用提交按钮,如下所示:
<button class="g-recaptcha" data-sitekey="PASTE-YOUR-RECAPTCHA-SITE-KEY-HERE" data-callback="onSubmit" data-action="submit">Submit Form</button>并添加以下用于提交表单的JavaScript函数:
function onSubmit() {
var form = document.forms[0]; // change this if you have multiple forms
if (/* possible client-side form validation code here */) {
form.submit();
}
}服务器端验证代码:
这里有相关的文档:https://developers.google.com/recaptcha/docs/verify
为此,我创建了一个helper函数:
/**
* Checks if the current script has a valid Google Captcha response token.
* @returns True, if the script has a valid repsonse token, otherwise false.
*/
function isCaptchaValid()
{
$captcha = isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response'] : false;
if (!$captcha) {
return false;
}
$postdata = http_build_query(
array(
"secret" => "PASTE-YOUR-RECAPTCHA-SECRET-KEY-HERE",
"response" => $captcha,
"remoteip" => $_SERVER["REMOTE_ADDR"]
)
);
$opts = array(
'http' =>
array(
"method" => "POST",
"header" => "Content-Type: application/x-www-form-urlencoded",
"content" => $postdata
)
);
$context = stream_context_create($opts);
$googleApiResponse = file_get_contents("https://www.google.com/recaptcha/api/siteverify", false, $context);
if ($googleApiResponse === false) {
return false;
}
$googleApiResponseObject = json_decode($googleApiResponse);
return $googleApiResponseObject->success;
}不需要像在其他答案中那样检查任何分值。根据文档,response对象中甚至没有score属性。我检查过了,有一个,但我不用它。
您应该在处理表单提交的PHP脚本的开头调用它,如下所示:
if (!isCaptchaValid()) {
die("STOP! You are a bot."); // or do something else
}https://stackoverflow.com/questions/54183985
复制相似问题