Recaptcha v2 未在 PHP 中验证

Recaptcha v2 not validating in PHP

我正在尝试在我的 PHP 申请注册表中验证 google recaptcha V2。但它没有得到验证。它给出一条成功消息并将其重定向到登录页面。有人可以帮我解决这个问题吗?

HTML如下:

<div class="captcha_wrapper">
    <div class="g-recaptcha" data-sitekey="KEY"></div>
</div>

AJAX如下:

 <script>
     $(document).ready(function() {
         jQuery.validator.addMethod("noSpace", function(value, element) { 
             return value.indexOf(" ") < 0 && value != ""; 
         }, "Spaces are not allowed");

         $("#register_form").submit(function() {
             $("#register_form").validate({
                 rules: {
                     firstname: {
                         required: true
                     },
                     lastname: {
                         required: true
                     },
                     email: {
                         required: true,
                         email: true
                     },
                     username: {
                         required: true,
                         noSpace: true
                     },
                     password: {
                         required: true,
                         minlength: 6
                     },
                     retype_password: {
                         required: true,
                         equalTo: "#inputPassword"
                     },
                 },
                 messages: {
                     firstname: {
                         required: "Enter Firstname<br />",
                     },
                     lastname: {
                         required: "Enter Lastname<br />",
                     },
                     email: {
                         required: "Enter your email address",
                         email: "Enter valid email address"
                     },
                     username: {
                         required: "Enter Username<br />", 
                     },
                     password: {
                         required: "Enter your password<br />",
                         minlength: "Password must be minimum 6 characters"
                     },
                     retype_password: {
                         required: "Enter confirm password",
                         equalTo: "Passwords must match"
                     },
                 },
                 errorPlacement: function(error, element) {
                     error.hide();
                     $('.messagebox').hide();
                     error.appendTo($('#alert-message'));
                     $('.messagebox').slideDown('slow');
                 },
             });

    if ($("#register_form").valid()) {
    var data1 = $('#register_form').serialize();
    $.ajax({
        type: "POST",
        url: "register.php",
        data: data1,captcha: grecaptcha.getResponse(),
        success: function(msg) {
            console.log(msg);
            //check if response is true
            if (msg == true) {
                $('.messagebox').hide();
                $('#alert-message').html(msg);
                $('.messagebox').slideDown('slow');
                $("#btn").text('Please Wait...'); // a
                top.location.href = "index.php?msg=login"; //redirection
            } else {
                $('#alert-message').html("CATCHA VALIDATION FAIL!");
            }

        }
    });

             }

             return false;
        });
    });

    $("form").submit(function() {
        console.log($(this).serializeArray());
        console.log('captcha response: ' + grecaptcha.getResponse()); // --> captcha response: 
        return false;
    });
</script>

这是 PHP 页面,它正在验证 POST。

    if (isset($_POST['g-recaptcha-response'])) {

    $secret = '';
    //get verify response data
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);

    $responseData = json_decode($verifyResponse);

            if($responseData->success == true) {
                //Success: do code to store your data...
                echo 'Robot Verfification SUCCESS';
                return true;
            } else {
                echo 'Robot verification failed, please try again.';
                return false;   
            } 
    }

现在的问题是正确验证。但如果验证正确,则它必须重定向到 "top.location.href = "index.php?msg=login";"。但它没有重定向。

更新代码

//check for the post param
if (isset($_POST["captcha"])) {
    //PHP Code
    $secret = "";
    $response = $_POST["captcha"];
    $verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
    $captcha_success = json_decode($verify);
    if ($captcha_success->success == true || $captcha_success->success == "true" || $captcha_success->success == 1) {
        return true;
    } else {
        return false;
    }
}


    //jQuery
    if ($("#register_form").valid()) {
        var data1 = $('#register_form').serialize();
        $.ajax({
            type: "POST",
            url: "register.php",
            data: {
                'data1': data1,
                'captcha': grecaptcha.getResponse()
            },
            beforeSend: function() {
                $('.messagebox').slideDown('slow');
                $("#btn").text('Please Wait...'); // a
            },
            //captcha: grecaptcha.getResponse(),
            success: function(msg) {
                console.log(msg);
                //check if response is true
                if (msg == true) {
                    $('.messagebox').hide();
                    $('#alert-message').html("Validation Successful");
                    // $('.messagebox').slideDown('slow');
                    // $("#btn").text('Please Wait...'); // a
                    window.location.replace("https://my-site.com/index.php?msg=login"); //redirect to any URL
                    // top.location.href = "index.php?msg=login"; //redirection
                } else {
                    //if validation fails then do stuffs here
                    $('#alert-message').html("Validation Failed !");
                }

            }
        });