【问题标题】:Google ReCaptcha not posting 'g-recaptcha-response' on SOME pagesGoogle ReCaptcha 未在某些页面上发布“g-recaptcha-response”
【发布时间】:2016-05-04 21:17:30
【问题描述】:

这个问题我已经有一段时间了,我想不通。我的 Google recaptcha 代码似乎可以在某些网站上运行 - 但是当添加到其他网站(甚至同一网站内的其他页面)时,完全相同的代码将不起作用。 当它不起作用时,如果我执行 var_dump($_POST['g-recaptcha-response']); (在第二页上)我得到 NULL。

我的初始/表单代码:

<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="xxxxx"></div>

我的验证码:

$gRecaptcha = $_POST['g-recaptcha-response'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=xxxxx&response=".$gRecaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false || !$gRecaptcha){
    die('xxxx');
}

还有其他人也发布了这个问题,但似乎没有人发布解决方案(他们都只是切换到不同的验证码)。 有什么建议接下来要检查什么?

【问题讨论】:

    标签: php recaptcha


    【解决方案1】:

    if 子句中的条件是错误的。 API 响应是一个 json 对象,如下所示:

    {
      "success": true|false,
      "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
      "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
      "error-codes": [...]        // optional
    }
    

    这是参考:

    所以首先你必须使用json_decode() 函数对其进行解码,然后检查用户响应的状态。

    因此你的代码应该是这样的:

    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
        //get verified response data
    
        //your site secret key
        $secret = 'YOUR_SECRET_KEY';
    
        $gRecaptcha = $_POST['g-recaptcha-response'];
        $gRecaptcha = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
    
        $response = file_get_contents($gRecaptcha);
        $responseData = json_decode($response);
    
        if($responseData->success){
            // success
        }else{
            // failure
        }
    }
    

    【讨论】:

    • 我会切换它,但这不是问题。我从 g-recaptcha-response 的帖子中得到 NULL
    • 即使我遇到问题,您的代码也会让它通过,但那是因为您将所有内容都包装在 if(isset($_POST['g-recaptcha-response']) &amp;&amp; !empty($_POST['g-recaptcha-response'])){ 中,当它为空时它会完全忽略它,甚至让它通过如果根本没有检查recaptcha
    • @Devpaq if 条件在用户方响应时为真,否则不为真。做var_dump($_POST['g-recaptcha-response']);,是否显示NULL
    • @Devpaq 可能是this SO question 将帮助您进一步调试问题。请展示您如何显示 reCaptcha 小部件,我的意思是 HTML 表单。
    • var_dump($_POST['g-recaptcha-response']); 显示NULL 这就是我最初提出问题的原因。 recapthca 小部件基本上就是我在问题&lt;form name='orderInfo' id='orderInfo' method='post' action='&lt;?=htmlspecialchars($_SERVER['SCRIPT_NAME'])?&gt;'&gt; &lt;!-- other form inputs --&gt; &lt;div class="dn-onefull" align="center"&gt; &lt;script src='https://www.google.com/recaptcha/api.js'&gt;&lt;/script&gt; &lt;div class="g-recaptcha" data-sitekey="XXXXXX"&gt;&lt;/div&gt; &lt;/div&gt; 中展示的内容,正如我所提到的,相同的代码适用于某些页面/站点,而不适用于其他页面/站点。我想不出有什么矛盾的
    【解决方案2】:

    这样使用: 第1步 : 将此代码用于验证:

    <?php
    $secret = "Your own code";
    $sitekey = "Your own code";
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $captcha=$_POST['g-recaptcha-response'];
                if(!$captcha){
                    header("Location: index.php?info=cap");
                    exit;
                }
                $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
                if($response['success'] == false)
                {
                      header("Location: index.php?info=cap");
                        exit;
                }
    }
        ?>
    

    第 2 步:在表单中使用此标签:

    <label>I'm not robot: </label>
    <div class="g-recaptcha"></div>
    

    第 3 步:谷歌 API

    <script src="https://www.google.com/recaptcha/api.js?onload=CaptchaCallback&render=explicit" async defer></script>
    <script type="text/javascript">
        var CaptchaCallback = function(){
        $('.g-recaptcha').each(function(index, el) {
            grecaptcha.render(el, {'sitekey' : '<?php echo $sitekey;?>'});
            });
        };
    </script>
    

    【讨论】:

      【解决方案3】:

      验证您已在 google recaptcha 中添加了这两个:

      www.yourdomain.comyourdomain.com

      【讨论】:

      • 这不正确,它只是“你的域”,实际上你甚至不允许将 www、协议或 .com 部分放在域列表中。
      猜你喜欢
      • 2015-08-21
      • 2015-02-25
      • 1970-01-01
      • 2015-10-05
      • 2020-09-27
      • 2017-08-29
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      相关资源
      最近更新 更多