Google reCaptcha验证(siteverify)返回false

时间:2017-12-09 19:03:28

标签: php recaptcha

之后我尝试在我的网站上实现一个简单的代码来使用reCAPTCHA,但即使这个简单的代码也行不通。

问题是验证函数总是返回一个bool(false)值。 我已经多次检查了钥匙,但它们是正确的

A <- matrix(1:16, nrow = 4, ncol = 4)

<head> <script src="https://www.google.com/recaptcha/api.js" async defer></script> </head> <body> <?php require_once "recaptchalib.php"; ?> <?php if(isset($_POST['new_comment']) && !empty($_POST['new_comment'])): if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) : $secret = "mysecretkey"; var_dump ($_POST['g-recaptcha-response']); $ip = $_SERVER['REMOTE_ADDR']; $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response'].'&remoteip='.$ip); echo ("verifyresponse"); var_dump ($verifyResponse); $responseData = json_decode($verifyResponse, true); echo ("responseData"); var_dump ($ResponseData); if ($response != null && $response->success) : echo 'Form info successfully submitted'; else: $errMsg = 'Robot verification failed, please try again.'; echo $errMsg ; endif; else: $errMsg = 'Please click on the reCAPTCHA box.'; echo $errMsg ; endif; else: $errMsg = ''; $succMsg = ''; endif; ?> <form name="form1" action="thisfile.php" method="post"> Enter Comment :<br /> <textarea name="new_comment" cols="75" rows="10"></textarea><br /> <div class="g-recaptcha" data-sitekey="mysitekey"></div> <input type="submit" value="Envoyer" /> </form> </body> </html> 给出了预期的长字符链,但var_dump ($_POST['g-recaptcha-response'])给出var_dump ($verifyResponse)值,而我从未看到bool(false)

任何人都可以帮我弄清楚这段代码有什么问题吗?

我已将问题缩小到var_dump ($ResponseData)调用返回“false”值而非预期

的事实
  

{         “成功”:是的,          “challenge_ts”:“2017-12-10T15:09:12Z”,          “hostname”:“mydomain”      }

令我疯狂的是,如果回显整个字符串并直接在浏览器中复制,那么我得到正确答案(上图),所以我无法弄清楚出了什么问题...

这是当前'剥离到骨头'版本的代码(我已经删除了file_get_content调用以及之后的所有内容,因为只要我得到错误的返回值,我知道其余的将不起作用)

json_decode

2 个答案:

答案 0 :(得分:0)

检查变量:

if ($responseData!= null && $responseData['success']) {

您的代码的其他修改:

$secret = "Add Your Secret Key"; // may be you are adding here public key instead of private ? check it
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response']);
$response = json_decode($response, true); // if you add "true" then the response will be an array

if($response["success"] === true){
    echo "Form Submit Successfully.";
}else{
    echo "You are a robot";
}

另外,我建议您查看这些教程:

Google Recaptcha Tutorial

这个来自Github的回程示例:

require('/path/to/recaptcha/src/autoload.php');
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
    // verified!
    // if Domain Name Validation turned off don't forget to check hostname field
    // if($resp->getHostName() === $_SERVER['SERVER_NAME']) {  }
} else {
    $errors = $resp->getErrorCodes();
}

Github link

答案 1 :(得分:0)

我认为,失败的原因如下:

第一个问题是使用true作为json_encode的第二个参数 - 这会将数据放入数组格式,但您尝试使用Object语法来访问status属性返回的json字符串。

$responseData = json_decode($verifyResponse, true);

所以应该是

$responseData = json_decode($verifyResponse);

第二期 - 二合一在这里。 $response在哪里定义?第二个是如上所述 - 使用Object语法而不是数组..

if ($response != null && $response->success)

应该是(假设$responseData = json_decode( $verifyResponse );

if ( $verifyResponse != null && $responseData->success==1 )

以下是我的测试 - 或多或少相同但旧版本的php所以使用花括号而不是冒号样式..

<!doctype html>
<html>
    <head>
        <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    </head>
    <body>
        <?php

            define('GOOGLE_RECAPTCHA_KEY','xxxyyyzzz');
            define('GOOGLE_RECAPTCHA_SECRET_KEY','aaabbbccc');

            $message = false;

            if ( isset( $_POST['new_comment'], $_POST['g-recaptcha-response'] ) && !empty( $_POST['new_comment'] ) && !empty( $_POST['g-recaptcha-response'] ) ){

                $secret = GOOGLE_RECAPTCHA_SECRET_KEY;
                $ip = $_SERVER['REMOTE_ADDR'];
                $response = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response'] . '&remoteip=' . $ip );
                $json = json_decode( $response );

                /* 
                    You need to use $json->success not $response->success 
                    and because you are trying to use object syntax rather
                    than array syntax json_decode does not need 2nd param `true`
                */
                if( $response && !is_null( $response ) && $json->success==1 ){
                    $message='Form info successfully submitted';
                }else{
                    $message = 'Robot verification failed, please try again.';
                }
            }
        ?>

        <form method='post'>
            Enter Comment :<br />
            <textarea name='new_comment' cols='75' rows='10'></textarea>
            <?php
                if( !empty( $message ) )echo $message;
            ?>
            <br />
            <div class='g-recaptcha' data-sitekey='<?php echo GOOGLE_RECAPTCHA_KEY;?>'></div>
            <input type='submit' value='Envoyer' />
        </form>
    </body>
</html>