簡體   English   中英

Google reCAPTCHA V2 JavaScript我們檢測到您的網站未驗證reCAPTCHA解決方案

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

錯誤消息我們檢測到您的站點未驗證reCAPTCHA解決方案。 這是在您的網站上正確使用reCAPTCHA所必需的。 有關更多信息,請參閱我們的開發者網站 我創建了這個reCaptcha代碼,它運行良好,但我不知道如何驗證它,我認為它是通過函數grecaptcha.getResponse();驗證的grecaptcha.getResponse(); 但事實並非如此。 顯然,recaptcha在網站上運行良好,但我剛在谷歌管理員看到下一條消息: 在此輸入圖像描述

要求: 1。不要在表單中使用action="file.php" ,只在表單中使用javascript函數。

  <!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> 

求你幫幫我吧。 我會很感激我如何驗證它,因為我需要使用一個javascript函數onsubmit="return get_action();" 而不是action="file.php" *當我提交?:

grecaptcha.getResponse()函數只會為您提供用戶響應令牌,然后必須使用google reCAPTCHA服務器上的HTTP POST調用進行驗證。

您可以使用AJAX請求來驗證令牌,但出於安全原因,這些驗證應該始終在服務器端完成 - JavaScript可能總是被用戶干擾並且被欺騙相信reCAPTCHA已成功驗證。

Google reCAPTCHA文檔

獲得響應令牌后,您需要使用以下API使用reCAPTCHA對其進行驗證,以確保令牌有效。

因此,您需要做的是將reCAPTCHA秘密(在reCAPTCHA管理頁面中為您生成的第二個密鑰)和用戶響應令牌(從grecaptcha.getResponse()函數接收的grecaptcha.getResponse() )發送到reCAPTCHA API,如reCAPTCHA文檔中所述

在PHP中 ,你會做這樣的事情:

$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"}
}

這就是我的建議......

1.)在reCPATCHA腳本中附加查詢字符串。 另外,為了提高性能,防止渲染阻塞,您可以將<script ...> async defer屬性添加到<script ...> 為了正確,您應該將腳本移動到DOM中的<form>之后。 ...</form> <script src='https://www.google.com/recaptcha/api.js?onload=recaptchaRender' async defer></script>

2.)添加一個名為recaptchaRender()的新函數。 要處理您對Google的驗證並將響應卸載到您回調get_action RECAPTCHA_SITE_KEY替換為您的Google網站密鑰。

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

3.)將id="reCaptchSubmit"添加到提交按鈕。

4.)刪除onsubmit="return get_action();" 來自你的表單元素。

基本上你錯過了你的腳本和谷歌之間的身份驗證。 這不會處理您的表單驗證,但至少會讓您進入渲染回調get_action() ,您可以在其中處理驗證 - 可能想要重命名為更相關的東西,即validateForm()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM