[英]ValidationError: “g-recaptcha-response” is not allowed
[英]phpmailer with recaptcha v3 doesn't send email - Undefined index: g-recaptcha-response
我在用phpmailer實現Google驗證碼v3時遇到問題。 當我填寫表格並單擊發送時。 發生錯誤:注意:未定義索引:第6行的form.php中的g-recaptcha-response錯誤!驗證碼有問題,您對我們撒謊! 你是機器人! 或您只是沒有單擊它:)錯誤告訴您錯誤在form.php的第6行中,因此:JS文件
<script src='https://www.google.com/recaptcha/api.js?
render=6LemuWIUAAAAAATQOAxYz-30Uf8VbXery0I9J8ZA'></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('6LemuWIUAAAAAATQOAxYz-30Uf8VbXery0I9J8ZA', {
action: 'form'
})
});
</script>
PHP文件:
<?php
date_default_timezone_set('Etc/UTC');
ini_set('display_errors',1); error_reporting(E_ALL);
if(isset($_POST['submit'])){
$userIP = $_SERVER["REMOTE_ADDR"];
$recaptchaResponse = $_POST['g-recaptcha-response'];
$secretKey = 'abc...';
$request = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secretKey}&response={$recaptchaResponse}&remoteip={$userIP}");
if(!strstr($request, "true")){
echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong>There was a problem with the Captcha, you lied to us! you are a robot! or you just didnt click it :)</div>';
}
else{
if(isset($_POST['submit']))
{
$message=
'name: '.$_POST['name'].'<br />
email: '.$_POST['email'].'<br />
mess: '.$_POST['message'].'
';
require "class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "abc...";
$mail->Port = 465;
$mail->Encoding = '7bit';
$mail->SMTPDebug = 3;
$mail->Username = 'a@a.pl';
$mail->Password = 'abc';
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->WordWrap = 50;
$mail->MsgHTML($message);
$mail->AddAddress('a@a.com');
$result = $mail->Send();
$message = $result ? '<div class="alert alert-success" role="alert">
<strong>Success!</strong>Message Sent Successfully!</div>' : '<div
class="alert
alert-danger" role="alert"><strong>Error!</strong>There was a problem
delivering the message.</div>';
unset($mail);
}
}
}
HTML檔案:
<form action="form.php" method="POST" class="contact__form g-recaptcha"
id="form" data-sitekey="6LdvpmIUAAAAABEO5KGWX1KsIJgPQnZyAJep4lkw">
<div class="form__div">
<label for="name">Name </label>
<input type="text" name="name" id="name" class="input input__name" required/>
</div>
<div class="form__div">
<label for="email">E-mail </label>
<input type="email" name="email" id="email" class="input input__email " required/>
</div>
<div class="form__div">
<label>Message </label>
<textarea rows="5" aria-label="Write something" name="message" class="input input__mess" placeholder="Write..." minlength="10" maxlength="1000" required></textarea>
</div>
<button name="submit" type="submit" id="submit" class="form__button">Wyślij</button>
</form>
這與PHPMailer無關-在運行任何PHPMailer代碼之前都會發生該錯誤。
您正在$_POST
數組中尋找一個名為g-recaptcha-response
,但該值不存在,因此會出現錯誤。 您沒有具有該名稱的input元素,這就是為什么$_POST
缺少該元素。 可能是Google代碼從JS動態添加了它,但是在您發布的內容中沒有證據表明這樣做。 我建議您多閱讀recaptcha文檔。
這里不是一個特別的問題,但是您正在運行一個非常舊的PHPMailer版本,並且您的代碼基於一個過時的示例。
您沒有將令牌傳遞給請求。 您必須在表單中設置令牌值。 你可以做這樣的事情。
grecaptcha.ready(function() {
grecaptcha.execute("KEY_VALUE", {action: 'form'})
.then(function(token) {
localStorage.setItem('recaptcha_token', token)
$('form').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token + '">');
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.