隐形reCAPTCHA缺少输入响应

时间:2017-10-30 16:01:57

标签: javascript php invisible-recaptcha

我正在尝试制作一个隐藏reCAPTCHA的网页,但我无法让它工作。

客户页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://code.jquery.com/jquery-2.2.1.min.js" integrity="sha384-8C+3bW/ArbXinsJduAjm9O7WNnuOcO+Bok/VScRYikawtvz4ZPrpXtGfKIewM9dK" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    <script>
        $('#formcomplete').click(function (e) {
            grecaptcha.reset();
            document.getElementById('form').checkValidity();
            grecaptcha.execute();
            return false;
        });
        function onSubmit(token) {
            document.getElementById('form').submit();
        }
    </script>
    <script src='https://www.google.com/recaptcha/api.js' async defer></script>
    <style>
        .centerize {
            position: absolute;
            left: 50%;
            transform: translate(-50%, 0);
        }
    </style>
</head>
<body style="background-color: black; overflow: hidden;">
    <form method='post' action='_resources/signup.php' id="form">
        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); max-width: 50%;">
            <div class="form-row">
                <div class="form-group col-md-5">
                    <input type="text" class="form-control" name="name" placeholder="Name" size="64" required>
                </div>
                <div class="form-group col-md-7">
                    <input type='email' class='form-control' name='email' placeholder='email@example.com' size="64" required>
                </div>
            </div>
            <div class="form-check" style="text-align: center;">
                <label class="form-check-label" style="color: white">
                    <input type="checkbox" class="form-check-input" name="emaillist" value="emaillisttrue">
                    Add me to the email list to learn about future releases.
                </label>
            </div>
            <div class="form-check" style="text-align: center;">
                <label class="form-check-label" style="color: white">
                    <input type="checkbox" class="form-check-input" name="tac" value="tactrue" required>
                    I accept the <a href="./legal.html" onclick="window.open(this.href, 'mywin', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); return false;" >Terms and Conditions & Privacy Policy</a>.
                </label>
            </div>
            <button class='btn btn-default centerize' id="formcomplete">Submit</button>
        </div>
        <div id='recaptcha' class="g-recaptcha"
             data-sitekey="keeeeey"
             data-callback="onSubmit"
             data-theme="dark"
             data-size="invisible">
        </div>
    </form>
</body>

和服务器端验证:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $emaillist = stripslashes($_POST['emaillist']);
    $tac = stripslashes($_POST['tac']);
    //... verify that you have a name email and accepted the terms
    $verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify",false,stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'content' => http_build_query(array(
                'secret' => 'keeeeey',
                'response' => $_POST['g-recaptcha-response'],
                'remoteip' => $_SERVER['REMOTE_ADDR']
            ))
        )
    )));
    if (json_decode($verify)->success==true) {
        //... handle normal case
    } else {
        var_dump($verify);
        //header("Location: ...");
    }
}

当我浏览页面时,所有内容都显示正常,输入已经过验证(Chrome显示“如果你没有,请选中此复选框继续”),一切正常,直到我点击提交。点击提交后,我会被定向到表单的POST端点,var_dump会导致令人沮丧的string(75) "{ "success": false, "error-codes": [ "missing-input-response" ] }"

现在让我感到困惑。如果我浏览页面,请填写所有详细信息,然后打开chrome dev控制台并输入grecaptcha.execute();表单已提交,一切正常好的,reCAPTCHA请求正常,并且在成功的验证路径服务器端发生的所有事情都会发生。我不知道为什么会这样。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

移动

<script>
        $('#formcomplete').click(function (e) {
            grecaptcha.reset();
            document.getElementById('form').checkValidity();
            grecaptcha.execute();
            return false;
        });
        function onSubmit(token) {
            document.getElementById('form').submit();
        }
    </script>
    <script src='https://www.google.com/recaptcha/api.js' async defer></script>

在结束body标签之前以某种方式工作。在尝试使用DOM或其​​他东西之前,可能需要定义DOM。