Google reCAPTCHA V2 JavaScript We detected that your site is not verifying reCAPTCHA solutions

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 06:06:27

问题


Error Message We detected that your site is not verifying reCAPTCHA solutions. This is required for the proper use of reCAPTCHA on your site. Please see our developer site for more information. I created this reCaptcha code, it works well but I don not know how can I validate it, I thought it was validating with the function grecaptcha.getResponse(); but it was not so. Apparently The recaptcha worked well on the website but I just saw in Google admin the next message:

Requirements: 1.Do not use action="file.php" in the form, only a javascript function in the form.

     <!DOCTYPE>
<html>
<head >
    <title></title>
    <script src='https://www.google.com/recaptcha/api.js'></script>
    <script type="text/javascript">
        function get_action() {
            var v = grecaptcha.getResponse();
            console.log("Resp" + v );
            if (v == '') {
                document.getElementById('captcha').innerHTML = "You can't leave Captcha Code empty";
                return false;
            }
            else {
                document.getElementById('captcha').innerHTML = "Captcha completed";
                return true;
            }
        }
    </script>
</head>
<body>
    <form id="form1" onsubmit="return get_action();">
    <div>
    <div class="g-recaptcha" data-sitekey="6LdNIlAUAAAAADS_uVMUayu5Z8C0tw_jspdntbYl"></div>
    </div>
   <input type="submit" value="Button" />
   
    <div id="captcha"></div>
    </form>
</body>
</html>

May you help me Please. I would appreciate it a lot How can I validate it since I need to use a javascript function onsubmit="return get_action();" instead of action="file.php" *when I submit?:


回答1:


The grecaptcha.getResponse() function will only provide you with the user response token, which then must be validated with HTTP POST call on google reCAPTCHA server.

You could use AJAX request, to validate the token, but these validations should always be done on server side, for security reasons - JavaScript could've always been meddled with by user and tricked into believing that reCAPTCHA was successfully verified.

Google reCAPTCHA docs:

After you get the response token, you need to verify it with reCAPTCHA using the following API to ensure the token is valid.

So what you need to do is to send your reCAPTCHA secret (second key that was generated for you in reCAPTCHA admin page) and user response token (the one received from grecaptcha.getResponse() function) to reCAPTCHA API as described in reCAPTCHA docs.

In PHP, you'd do somethink like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'secret'   => YOUR_RECAPTCHA_SECRET,
    'response' => USER_RESPONSE_TOKEN,
]));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($ch);

curl_close($ch);

$response = @json_decode($data);

if ($response && $response->success)
{
    // validation succeeded, user input is correct
}
else
{
    // response is invalid for some reason
    // you can find more in $data->{"error-codes"}
}



回答2:


This is what I would suggest...

1.) Append a query string to your reCPATCHA script. Also for better performance, prevents render blocking, you can add async defer attributes to your <script ...>. And for correctness you should move the script to after the <form> in the DOM. ...</form> <script src='https://www.google.com/recaptcha/api.js?onload=recaptchaRender' async defer></script>.

2.) Add a new function called recaptchaRender(). To handle your validation to Google and offload the response to you callback get_action. Replace RECAPTCHA_SITE_KEY to with your Google site key.

function recaptchaRender{
    grecaptcha.render( 'reCaptchSubmit', {
        'sitekey': 'RECAPTCHA_SITE_KEY',
        'callback': get_action
    });
}

3.) Add id="reCaptchSubmit" to your submit button.

4.) Remove onsubmit="return get_action();" from your form element.

Essentially you were missing the authentication between your script and Google. This doesn't handle your forms validation, but will at least get you to your render callback get_action(), where you can handle validation - might want to rename to something more relevant i.e. validateForm() etc



来源:https://stackoverflow.com/questions/49592772/google-recaptcha-v2-javascript-we-detected-that-your-site-is-not-verifying-recap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!