【问题标题】:missing-input-response | Invisible reCaptcha缺少输入响应 |隐形验证码
【发布时间】:2017-08-07 19:58:53
【问题描述】:

因此,我正在尝试在某些网站上实施 Google 全新的 Invisible reCaptcha。

我完全按照这些步骤操作,但它不断给我缺失输入响应错误。

HTML 代码:

<form id="subscribe-form" class="form-inline" action="phpScripts/subscribe/subscribeHandler.php" method="post">
    <div class="input-group">
        <input type="email" name="email" class="form-control" size="50" placeholder="Email Address" required>
        <div class="input-group-btn">
            <button class="g-recaptcha btn btn-danger" data-sitekey="6LfoNhkUAAAAAEcQFx8vGHZKHXsZ_q0j2KDnmU9M" data-callback="submitForm">Subscribe</button>
        </div>
    </div>
</form>

PHP 代码:

<?php
include 'databaseConnection.php';
if($_POST){
            $secret = "MY SECRET KEY";
            $captcha= $_POST['g-recaptcha-response'];
            $ip = $_SERVER['REMOTE_ADDR'];
            $url= file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip=$ip");
            print_r($url);
            $decodedResponse = json_decode($url, TRUE);

            if($decodedResponse['success'] == 1){//code here}

所以,我认为我的 $captcha 变量无法从 g-recaptcha-response 的 POST 中“捕获”任何内容。但是,为什么,这正是 Google 所说的,与旧的 reCaptcha v2 完全一样。

另外,我也加入了&lt;script src='https://www.google.com/recaptcha/api.js'&gt;&lt;/script&gt;

【问题讨论】:

    标签: php html post recaptcha invisible


    【解决方案1】:

    我在几个小时内都面临同样的问题,直到我终于明白了这个“隐形验证码”背后的逻辑! 您没有得到响应的原因仅仅是因为有一个空的 textarea 元素,其 id 和名称为 g-recaptcha-response

    此文本区域仅在挑战完成后填充响应字符串(这通常发生在通常的 recaptcha 中),但对于“invisible-captcha”,您必须使用“submit”显式调用 grecaptcha.execute();按钮,然后填充文本区域,并自动提交您的表单(假设您已将提交与callback 函数绑定)。

    就我而言,我已经让 php 处理表单和重新验证验证,所以我决定坚持使用旧的“复选框”版本(至少在它得到改进之前),因为我意识到改变一切真的很烦人(表单提交逻辑、按钮操作和 JavaScript 代码)只是为了隐藏一个复选框!特别是这两种方法几乎相同!

    【讨论】:

      【解决方案2】:

      问题可能是您可能将功能绑定到按钮。

      尝试实现他们在创建密钥时提供的代码:

      <form id="subscribe-form" class="form-inline" action="phpScripts/subscribe/subscribeHandler.php" method="post">
          <div class="input-group">
              <input type="email" name="email" class="form-control" size="50" placeholder="Email Address" required>
              <div class="g-recaptcha" data-sitekey="{keyhere}"></div>  
              <div class="input-group-btn">
                  <button class="btn btn-danger" data-sitekey=" data-callback="submitForm">Subscribe</button>
              </div>
          </div>
      </form>
      

      对于 PHP 逻辑验证:

      if ( $_POST['g-recaptcha-response'] ) {
      $secret = '{keyhere}';
      $response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR'] );
              $response = json_decode( $response );
              if ( ! $response->success ) {
                  //return error
              }
      
              //code logic below
      }
      

      在创建键时提供的 div 代码应该从它们的末端正确生成所有 HTML,以便在提交表单时能够由 PHP 验证处理。

      【讨论】:

        【解决方案3】:

        这里有一个解决方案。

        • 客户端
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>reCaptcha</title>
        
            <!--api link-->
            <script src="https://www.google.com/recaptcha/api.js" async defer></script>
            <!--call back function-->
            <script>
                function onSubmit(token) {
                    document.getElementById('reCaptchaForm').submit();
                }
            </script>
        </head>
        <body>
        <div class="container">
            <form id="reCaptchaForm" action="signup.php" method="POST">
                <input type="text" placeholder="type anything">
                <!--Invisible reCaptcha configuration-->
                <button class="g-recaptcha" data-sitekey="<your site key>" data-callback='onSubmit'>Submit</button>
                <br/>
            </form>
        </div>
        </body>
        </html>
        
        • 服务器端验证:创建 signup.php 文件
        <?php
        //only run when form is submitted
        if(isset($_POST['g-recaptcha-response'])) {
            $secretKey = '<your secret key>';
            $response = $_POST['g-recaptcha-response'];     
            $remoteIp = $_SERVER['REMOTE_ADDR'];
        
        
            $reCaptchaValidationUrl = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$response&remoteip=$remoteIp");
            $result = json_decode($reCaptchaValidationUrl, TRUE);
        
            //get response along side with all results
            print_r($result);
        
            if($result['success'] == 1) {
                //True - What happens when user is verified
                $userMessage = '<div>Success: you\'ve made it :)</div>';
            } else {
                //False - What happens when user is not verified
                $userMessage = '<div>Fail: please try again :(</div>';
            }
        }
        ?>
        <!DOCTYPE html>
        <html>
            <head>
                <meta charset="UTF-8">
                <title>reCAPTCHA Response</title>
            </head>
            <body>
                <?php
                    if(!empty($userMessage)) {
                        echo $userMessage;
                    }
                ?>
            </body>
        </html>
        

        【讨论】:

          【解决方案4】:

          每次执行后使用 grecaptcha.reset() 重置 recaptcha,一切正常。关注this link 了解更多关于 grecaptcha.reset() 的信息。

          【讨论】:

          • 请写一些描述。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-16
          • 2020-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-03
          • 1970-01-01
          相关资源
          最近更新 更多