php – Invisible ReCaptcha Validation in Ajax Form with JQuery Validation Plugin-ThrowExceptions

Exception or error:

Hello I have a problem with Invisible Recaptcha beceause I can’t validate most of the requests.
I have searched a lot in the forum but I couldn’t find any solution yet.
I am using JQuery Validation Plugin to validate the form and AJAX.
I know it’s a silly question but I really need your help to see what I am missing.
Any help will be really appreciated.

I have a form called ‘contact’ and at the end of it I have my submit

  <button class="btn btn-lg btn-light g-recaptcha" data-sitekey="SITE-KEY"
  data-callback="onSubmit">Send.</button>

Then my JQuery function onSubmit is:

function onSubmit(token) {
    if (($('#contact')).valid()) {
        $('#contact').submit();
    }
    else {
        $("#contact").addClass("shake animated"), function() {
            $(this).removeClass();
        });
        grecaptcha.reset();
    }
}

The AJAX is at the end of the JQuery validation settings as shown here:

$(document).ready(function() {
    $(function () {
        $.validator.setDefaults({
            errorClass: 'help-block'
        });
        $("#contact").validate({
            rules: {
                name: {
                    required: true,
                    lettersonly: true
                }
            },
            submitHandler: function () {
                $.ajax({
                    type: "POST",
                    url: "process.php",
                    data: $('#contact').serialize(),
                    success: function () {
                        $("#fields").prop('disabled', true);
                    },
                    error: function () {
                        $('#msgSubmit').html("Error");
                    }
                });
                return false;
            }
        });
        });
    });

And my server side verification is:

    <?php
$url = "myurl";
if (isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD']) == 'POST') {
    if(@isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']==$url) {
        $res = post_captcha($_POST['g-recaptcha-response']);
        if (!$res['success']) {
            echo '<br><p>Error.</p><br>';
        } else {
            echo 'OK';
        }
    }
} else {
    die();
}

function post_captcha($user_response) {
    $fields_string = '';
    $fields = array(
        'secret' => 'PRIVATE-KEY',
        'response' => $user_response
    );
    foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
    $fields_string = rtrim($fields_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result, true);
}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *