如何在Google reCAPTCHA V3
<!--前端:-->
<script src="https://www.google.com/recaptcha/api.js?render=*******公鑰*******"></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('*******公鑰*******', {action: 'homepage'}).then(function(token) {
var recaptchaResponse = document.getElementById('recaptchaResponse');
recaptchaResponse.value = token;
});
});
</script>
<!--表單中加上-->
<input type="hidden" value="" name="recaptcha_response" id="recaptchaResponse">
//*************************************************************
//後端:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
//密鑰
$recaptcha_secret = ”******密鑰******“;
//金鑰
$recaptcha_response = $_POST['recaptcha_response'];
//Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
//如果驗證成功,就進一步計算使用者分數,官方回饋分數為 0-1,分數愈接近 1 就是正常,低於 0.5 以下就有可能是機器人了
if($recaptcha->success==true)
{
if($recaptcha->score >= 0.5)
{
//echo 'Pass !';
}
else
{
showMsgRedirect($arrLangContact['_CONTACT_SEND_ERR'].$arrLangCommon['_CAPTCHA_CODE_ERR_MSG'], $errurl);
}
}
else
{
//echo 'Connection failed';
showMsgRedirect($arrLangContact['_CONTACT_SEND_ERR']."Connection failed", $errurl);
}