两个reCaptcha在一个页面上,如何在php中验证


Two reCaptcha on one page, how to verify in php?

我试图在一个有两个表单的页面上使用2x reCapcha。我设法把它们分开显示,但我现在无法验证数据。为了验证,我使用了两个独立的功能,比如这个

$recaptcha = $_POST['g-recaptcha-response'];
if(!empty($recaptcha)) {
    $google_url = "https://www.google.com/recaptcha/api/siteverify";
    $secret = 'secret';
    $ip = $_SERVER['REMOTE_ADDR'];
    $url = $google_url."secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
    $res = getCurlData($url);
    $res = json_decode($res, true);
    ...
    if($res['success']) { do stuff }

要加载我的2个capchas,我使用

<script src="//www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
<script type="text/javascript">
  var recaptcha1;
  var recaptcha2;
  var myCallBack = function() {
    //Render the recaptcha1 on the element with ID "recaptcha1"
    recaptcha1 = grecaptcha.render('recaptcha1', {
      'sitekey' : 'key', //Replace this with your Site key
      'theme' : 'light'
    });
    //Render the recaptcha2 on the element with ID "recaptcha2"
    recaptcha2 = grecaptcha.render('recaptcha2', {
      'sitekey' : 'key', //Replace this with your Site key
      'theme' : 'light'
    });
  };
</script>

有人能帮我吗?我该怎么做?这可能吗?

你在做一些棘手的事情。

首先,你需要清楚reCaptcha是如何工作的。现在,我将引导您了解如何插入它并在客户端进行验证。

1.在谷歌注册-您将拥有相同的网站密钥和两个reCaptchas的密钥。

2.在您的网站上插入一个带有CAPTCHA的表格-您需要一个带有重述的表格,有2个不同的回调来验证每个回调。

形式:

<script src="https://www.google.com/recaptcha/api.js" >;
<form method="post">
<div class="g-recaptcha" data-sitekey="[site_key]" data-callback="onReturnCallback1" data-theme="light"></div>
<div class="g-recaptcha" data-sitekey="[site_key]" data-callback="onReturnCallback2" data-theme="dark"></div>
<input value="submit" type="submit" />
</form>

3.解码响应的JavaScript代码-您需要定义2个回调来分别验证每个reCaptcha。完成后,您将验证结果存储在JS变量中。

<script> src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var success1=null, success2=null;
var onReturnCallback1 = function(response) { 
    //alert('g-recaptcha-response: ' + grecaptcha.getResponse()); 
    var url='proxy.php?url=' + 'https://www.google.com/recaptcha/api/siteverify';  
    $.ajax({ 'url' : url, 
               dataType: 'json',
               data: { response: response},
               success: function( data  ) {                     
               var res = data.success.toString();                   
               if (res ==  'true') { 
                        success1=true;
                        if (success1&&success2) { 
                             // do form submission
                        } 
                    } 
               } // end of success: 
         }); // end of $.ajax 
}; // end of onReturnCallback 
</script>

在每次回调中,您将成功结果保存在success1&success2并检查两者是否都成功强制形成子位置:

if (success1&&success2) { 
    // do form submission
} 

4.服务器上返回true或false的代理-似乎每个reCaptchas只需要一个代理文件即可验证站点。

if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
    header('HTTP/1.0 403 Forbidden');
    die('You are not allowed to access this file.');     
}
header('Content-type: application/json');
$url=$_GET['url'];
$response=$_GET['response'];
$secret = "[secter_key]"; 
$params = array('secret'=&gt; $secret, 'response'=&gt; $response);
$json=file_get_contents( $url  . '?secret=' . $secret . '&amp;response=' . $response);
echo $json;