javascript - “缺少输入响应”错误recaptcha v3
问题描述
我正在创建一个表单来发送带有recaptcha 的电子邮件。一切正常,但我注意到recaptcha v3 只持续了 3 分钟,需要重置。从那里开始出现“缺少输入响应”错误。
索引.php
<script>
grecaptcha.ready(function() {
grecaptcha.execute('key', {action: 'homepage'}).then(function(token) {
document.getElementById('g-recaptcha-response').value=token;
});
});
</script>
<script>
var callback = function() {
grecaptcha.render('id-of-render-element', {
'sitekey': 'key',
'expired-callback': expCallback
});
};
var expCallback = function() {
alert("Your recatpcha has expired, please verify again ...");
setInterval(function(){ grecaptcha.reset(); }, 5 * 60 * 1000 );
};
</script>
<div id="id-of-render-element"></div>
<script src="https://www.google.com/recaptcha/api.js?onload=callback&render=explicit" async defer></script>
类验证码
<?php
class Captcha{
public function getCaptcha($SecretKey){
$Resposta = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=key&response={$SecretKey}");
$Retorno = json_decode($Resposta);
return $Retorno;
}
public function returnCaptcha(){
echo "entrou calss_captcha";
$EnviaMail = False;
$ObjCaptcha = new Captcha();
$Retorno=$ObjCaptcha->getCaptcha($_POST['g-recaptcha-response']);
var_dump($Retorno);
if($Retorno->success == true && $Retorno->score > 0.5){
$EnviaMail = True;
}else{
$EnviaMail = False;
}
return $EnviaMail;
}
}
?>
解决方案
根据适用于版本 2 的用户评论。您需要为 file_get_contents 函数调用指定其他参数,如果您的站点具有 SSL,则设置上下文选项。
class Captcha{
public function getCaptcha($SecretKey){
if($SecretKey){
// Input data
$secret = 'SECRET_KEY';
$response = $SecretKey;
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify";
$post_data = http_build_query(
array(
'secret' => $secret,
'response' => $response,
'remoteip' => $remoteip
)
);
$options=array(
// If site has SSL then
'ssl'=>array(
// In my case its /etc/ssl/certs/cacert.pem
'cafile' => '/path/to/cacert.pem',
'verify_peer' => true,
'verify_peer_name' => true,
),
'http' =>array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create( $options );
$Resposta = file_get_contents( $url, false, $context );
$Retorno = json_decode($Resposta);
return $Retorno;
}
}
public function returnCaptcha(){
echo "entrou calss_captcha";
$EnviaMail = False;
$ObjCaptcha = new Captcha();
$Retorno=$ObjCaptcha->getCaptcha($_POST['g-recaptcha-response']);
var_dump($Retorno);
if($Retorno->success == true && $Retorno->score > 0.5){
$EnviaMail = True;
}else{
$EnviaMail = False;
}
return $EnviaMail;
}
}
推荐阅读
- angular - Angular HTTP 参数返回数组而不是在获取请求中重复
- datatables - DataTable:当我通过 ajax 在页面中加载其他表时,我得到了重复的固定标题
- blazor-server-side - Blazor 服务器端,在呈现时从 .razor 页面调用驻留在 CDN 中的 JavaScript 函数
- reactjs - 如何在 Flask 和 React 的页面刷新之间保持用户登录
- javascript - 如何在未聚焦的标签上进行网页抓取 [Chrome 扩展程序]
- spring-boot - 错误 org.springframework.boot.SpringApplication - 应用程序运行失败
- javascript - 无法发送除
- mysql - 事务中的触发器和 MEMORY 表不起作用?
- css - Laravel 7:浏览器看到编译的 CSS 文件但不应用它们
- c# - 无法从 BlobItem 检索元数据