dqx76962 2017-12-09 19:03
浏览 218

Google reCaptcha验证(siteverify)返回false

I've tried to implement a simple code to use reCAPTCHA on my site afterwards, but even this simple code doesn't work.

The problem is that the verify function always returns a bool (false) value. I've checked the keys several times but they are the correct ones

<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>

The var_dump ($_POST['g-recaptcha-response']) gives the expected long chain of characters, but the var_dump ($verifyResponse) gives a bool(false) value, and I never see the var_dump ($ResponseData).

Can anyone help me figure out what is wrong with this code?

I've narrowed down the problem to the fact that the file_get_content call returns a "false" value instead of the expected

{ "success": true, "challenge_ts": "2017-12-10T15:09:12Z", "hostname": "mydomain" }

What is getting me crazy is that if echo the full string and copy it directly in the browser then I get the correct answer (above), so I can't figure out what is going wrong...

Here's the current 'stripped to the bone' version of my code (I've deleted the json_decode call and everything after that since as long as I get a wrong return value I know the rest won't work)

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

        define('GOOGLE_RECAPTCHA_KEY','xxx');
        define('GOOGLE_RECAPTCHA_SECRET_KEY','yyy');

        $message = false;

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

      echo 'Sauvegarde du commentaire' ;
            $secret = GOOGLE_RECAPTCHA_SECRET_KEY;
            $ip = $_SERVER['REMOTE_ADDR'];
                            var_dump($_POST['g-recaptcha-response']);

            $getResponse = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response'];
                            var_dump($getResponse);

            $GoogleResponse = file_get_contents($getResponse) ;
                            var_dump($GoogleResponse) ;

      }

    ?>

    <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>
  • 写回答

2条回答 默认 最新

  • drdyszuy488152 2017-12-09 19:15
    关注

    Check you variables:

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

    Additional edits for your code:

    $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";
    }
    

    Also I advise you to look to these tutorial:

    Google Recaptcha Tutorial

    And this recaptcha example from 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

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法