如何解决Google v3 reCaptcha超时? [英] How to solve Google v3 reCaptcha timeout?

查看:305
本文介绍了如何解决Google v3 reCaptcha超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个PHP表单,其中有几个选项卡,并且reCaptcha超时.一切都在一页上完成,如果表单在3分钟内完成,则可以很好地完成工作.

We have a PHP form that is several tabs and times-out on the reCaptcha. Everything is done in one page and it works perfectly fine IF the form is completed in <3 minutes.

解决方案的想法是将表单处理和reCaptcha移至辅助页面进行处理.

The idea of a solution is to move the form processing and reCaptcha to a secondary page for processing.

问题在于表单页面会轮询Google服务以获取reCaptcha,并将令牌值收集到隐藏字段中.

The problem is that the form page polls the google service for reCaptcha and collects a token value to a hidden field.

<input type="hidden" name="recaptcha_response" id="recaptchaResponse">

问题是如何在服务器端处理页面上请求此令牌?这是客户端表单页面上使用的代码.我需要以某种方式重新生成令牌值以应用为:

The problem is how to request this token on the server side processing page? Here is the code used on the client side form page. I need to somehow regenerate the token value to apply as :

$ recaptcha_response

$recaptcha_response

这是表单页面上的有效版本.无需确定从表单页面上发布令牌的要求,这很容易,只是不确定如何重新生成令牌以在服务器端页面上使用.

Here is the working version on the form page. It's easy to remove the requirement on Posting the token from the form page, just not sure how to regenerate the token to use on the server side page.

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recaptcha_response'])) {

// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = RECAPTCHA_SECRET_KEY;
$recaptcha_response = $_POST['recaptcha_response'];
$remoteip = $_SERVER['REMOTE_ADDR'];

// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response. '&remoteip='.$remoteip);
$recaptcha = json_decode($recaptcha);

// Take action based on the score returned:
if ($recaptcha->score >= 0.5) {

编辑添加: 将进行reCaptcha的初始化,直到Submit延迟计时问题,因为这似乎是一个选择:

EDIT TO ADD: Would making the initialization of the reCaptcha until Submit delay the timing issue since this seems to be an option:

https://developers.google.com/recaptcha/docs/v3

"2.在操作或页面加载时调用grecaptcha.execute"

"2. Call grecaptcha.execute on an action or when the page loads"

推荐答案

如果您不想过多更改代码,则另一种方法是将reCaptcha JavaScript包装在命名函数中,设置时间间隔并轮询该函数提示reCaptcha每三分钟向您的表单元素添加一个新令牌:

If you do not wish to change your code too much then an alternative approach would be to wrap the reCaptcha JavaScript in a named function, set up an interval and poll that function prompting reCaptcha to add a new token to your form element every three minutes:

function getReCaptcha(){
    grecaptcha.ready(function() {
       ...
     });
 }

 getReCaptcha();  // This is the initial call
 setInterval(function(){getReCaptcha();}, 150000);

这篇关于如何解决Google v3 reCaptcha超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆