Javascript reCaptcha小部件没有';t expire和expired回调从未调用

Javascript reCaptcha小部件没有';t expire和expired回调从未调用,javascript,captcha,recaptcha,Javascript,Captcha,Recaptcha,我无法将reCaptcha集成到ajax联系人表单中。仅在用户完成挑战后的前2分钟内有效。ReCaptcha在2分钟后过期。问题是小部件完好无损,不像“会话已过期。再次完成挑战”。用户无法执行任何操作,只能重新加载页面。此外,不会调用过期的回调(显然,因为小部件没有过期) 我可以在完成后设置2分钟的计时器来重置小部件。这是可行的,但它有点不成熟,如果谷歌选择在未来更改过期时间会怎么样。你知道为什么它不会过期吗 我尝试了集成小部件的两种方法(自动和显式) 编辑: 下面是代码,我制作了一个小示例ht

我无法将reCaptcha集成到ajax联系人表单中。仅在用户完成挑战后的前2分钟内有效。ReCaptcha在2分钟后过期。问题是小部件完好无损,不像“会话已过期。再次完成挑战”。用户无法执行任何操作,只能重新加载页面。此外,不会调用过期的回调(显然,因为小部件没有过期)

我可以在完成后设置2分钟的计时器来重置小部件。这是可行的,但它有点不成熟,如果谷歌选择在未来更改过期时间会怎么样。你知道为什么它不会过期吗

我尝试了集成小部件的两种方法(自动和显式)

编辑: 下面是代码,我制作了一个小示例html文件,并证明它不起作用

<html>
<head></head>

<body>

        <div id="google-recaptcha-widget"></div>

<script>
        var expiredCallback = function() {
            alert('expired!');
        }

        var recaptchaLoad = function() {

                grecaptcha.render('google-recaptcha-widget', {
                        'sitekey' : 'mysitekey'
                        'expired-callback': expiredCallback
                });

        }

</script>

<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaLoad&render=explicit" async defer></script>

</body></html>

var expiredCallback=函数(){
警报(‘过期’);
}
var recaptchaLoad=函数(){
grecaptcha.render('google-recaptcha-widget'{
“sitekey”:“mysitekey”
“过期回调”:expiredCallback
});
}

函数myCallbackMethod(){
//拯救世界
}
函数expiredCallback(){
//哦,reCAPTCHA过期了,黑客世界
}

我遇到了同样的问题,数据过期回调根本不起作用。因此,我的解决方案是使用PHP以编程方式使用来自json对象的“challenge_ts”时间戳检查是否已经过了2分钟。我在下面的代码中添加了注释,其中时间戳实际上正在被处理:

$post_data = http_build_query(
    array(
        'secret' => 'YOUR_SECRET_KEY',
        'response' => $_POST['g-recaptcha-response'],
        'remoteip' => $_SERVER['REMOTE_ADDR']
    )
    );
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $post_data
    )
);
$context  = stream_context_create($opts);
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
    $result = json_decode($response);
    // Storing the timestamp when the captcha was generated on the page
    $reCaptchaTS = $result->challenge_ts; 
    $date = new DateTime();
    // Setting the time for the captcha to expire for 2 minutes
    date_add($reCaptchaTS, date_interval_create_from_date_string('2 minutes'));
    if ($result->success) {
        // If 2 minutes have passed since the captcha was generated then it expires, generating an error. 
        if ($date > $reCaptchaTS) {
            $reCaptchaExpiredError = "<div class='alert alert-danger'><b>Error:</b> The reCAPTCHA has expired. Please try again.</div>";
        }
    }
$post\u data=http\u build\u查询(
排列(
'secret'=>'YOUR\u secret\u KEY',
“response”=>$\u POST['g-recaptcha-response'],
'remoteip'=>$\u服务器['REMOTE\u ADDR']
)
);
$opts=array('http'=>
排列(
'方法'=>'发布',
'header'=>'内容类型:application/x-www-form-urlencoded',
“内容”=>$post_数据
)
);
$context=stream\u context\u create($opts);
$response=file\u get\u contents($response)https://www.google.com/recaptcha/api/siteverify“,假,$上下文);
$result=json_decode($response);
//在页面上生成验证码时存储时间戳
$reCaptchaTS=$result->挑战;
$date=新的日期时间();
//设置验证码过期时间为2分钟
日期添加($reCaptchaTS,日期间隔,从日期字符串('2分钟')创建日期;
如果($result->success){
//如果自生成验证码后已过2分钟,则验证码将过期,并生成错误。
如果($date>$reCaptchaTS){
$RECAPTCHAEXPIDERROR=“错误:reCAPTCHA已过期。请重试。”;
}
}
因此,这里获取json时间戳并将其存储在变量$reCaptchaTS中。然后我通过增加2分钟来更新时间戳,这样我们就可以设置验证码过期的时间。然后,我创建一个$date并将当前时间存储在其中


之后,我们只需检查用户是否使用if($result->success)完成了验证码。最后,我们可以检查当前时间是否大于过期时间,这意味着验证码已经过期。此时,我生成了名为$reCaptchaExpiredError的错误消息,但您可以按照自己的意愿处理此错误。

我添加了代码,但它非常简单,仅举个例子。reCaptcha有一些问题。在代码中,您没有设置过期回调。不奇怪“它不起作用”这与过期回调无关。小部件不会过期。通常,小部件应该变成“会话过期”或类似的东西。为了简单起见,我没有在代码中添加过期回调。我的原始代码中有它。可能是@dana的重复。这是另一个问题。至少在会话过期时发生了一些事情,但对我来说,小部件根本没有指示会话已过期,。
$post_data = http_build_query(
    array(
        'secret' => 'YOUR_SECRET_KEY',
        'response' => $_POST['g-recaptcha-response'],
        'remoteip' => $_SERVER['REMOTE_ADDR']
    )
    );
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $post_data
    )
);
$context  = stream_context_create($opts);
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
    $result = json_decode($response);
    // Storing the timestamp when the captcha was generated on the page
    $reCaptchaTS = $result->challenge_ts; 
    $date = new DateTime();
    // Setting the time for the captcha to expire for 2 minutes
    date_add($reCaptchaTS, date_interval_create_from_date_string('2 minutes'));
    if ($result->success) {
        // If 2 minutes have passed since the captcha was generated then it expires, generating an error. 
        if ($date > $reCaptchaTS) {
            $reCaptchaExpiredError = "<div class='alert alert-danger'><b>Error:</b> The reCAPTCHA has expired. Please try again.</div>";
        }
    }