重新获取码v2在PHP中无效

问题描述 投票:0回答:1

我正试图在我的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>

这是验证POST的PHP页面。

    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";"。但它没有重定向。

php jquery ajax recaptcha
1个回答
0
投票

更新代码

//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 !");
                }

            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.