我试图将Google reCaptcha V3添加到表单中,我进行了搜索,发现了另一个问题的一些文章和一些代码片段。
每次提交表单时,我都会得到false
。
这是HTML代码:
<script src='https://www.google.com/recaptcha/api.js?render=My Website Key'></script>
</head>
JS代码:
<body>
<script>
$('#mailForm').submit(function() {
event.preventDefault();
grecaptcha.ready(function() {
grecaptcha.execute('The Website Key', {action: 'homepage'}).then(function(token) {
$('#mailForm').prepend('<input type="hidden" name="token" value="' + token + '">');
$('#mailForm').prepend('<input type="hidden" name="action" value="homepage">');
$('#mailForm').unbind('submit').submit();
});;
});
});
</script>
表格:
<form action='php/mail.php' method="post" id='mailForm' enctype='multipart/form-data'>
//Some inputs
</form>
PHP代码:
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
$secret = 'My Secret Key';
//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){
//Send the mail
}else{
echo 'Please check reCaptcha';
}
}else{
echo 'Please check reCaptcha';
}
为什么它不起作用并且我不断收到Please check reCaptcha
?