I am trying to validate google recaptcha V2 in my PHP application registration form. But its not getting validated. Its giving a success message and its redirecting to the login page. Can someone please help me on this.
The HTML is as follows:
<div class="captcha_wrapper"> <div class="g-recaptcha" data-sitekey="KEY"></div> </div>
The AJAX is as follows:
<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>
This is the PHP Page where its Validating the 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; } }
Now the issue is its validating correctly. But If the validation is correct then it has to redirect to “top.location.href = “index.php?msg=login”;”. But its not redirecting.
Advertisement
Answer
Updated Code
//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 !"); } } });