Как правильно добавить recaptcha3 в мою контактную форму.Как правильно добавить recaptcha3 в мою контактную форму.Код ниже работает до сих пор.Когда я щелкнул правой кнопкой мыши на «Исследовать», в консоли есть «Token», но я не могу связать его с моим кодом и кнопкой «Отправить».Я пробовал что-то подобное (ниже), но это не работает, и я не знаю, как подключить это к моему коду.
<?php
define('SITE_KEY', '*****************************');
define('SECRET_KEY','****************************');
if($_POST){
function getCaptcha($SecretKey){
$Response = file_get_contents("https://google.com/recaptcha/api/siteverify?secret=".SECRET_KEY."&response={$SecretKey}");
$Return = json_decode($Response);
return $Return;
}
$Return = getCaptcha($_POST['g-recaptcha-response']);
//var_dump($Return);
if($Return->success==true && $Return->score>0.5){
echo "Success!!";
}else{
echo "Robot";
}
}
require_once "mail-sending-script.php";
?>
<!DOCTYPE html>
<html>
HTML-код Следующий код работает нормально
<?php
define('SITE_KEY', '***************');
define('SECRET_KEY','*************');
require_once "mail-sending-script.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link href="style.css" rel="stylesheet" type="text/css"/>
<title>Contact Form</title>
<script src="js/jquery-3.4.1.js" type="text/javascript"></script>
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY ; ?>">
</script>
</head>
<script type="text/javascript">
function validate() {
var valid = true;
$(".info").html("");
var userName = document.forms["mailForm"]["userName"].value;
var userPhone = document.forms["mailForm"]["userPhone"].value;
var userEmail = document.forms["mailForm"]["userEmail"].value;
var subject = document.forms["mailForm"]["subject"].value;
var userMessage = document.forms["mailForm"]["userMessage"].value;
if (userName == "") {
$("#userName-info").html("(required)");
$("#userName").css('background-color', '#FFFFDF');
valid = false;
}
if (userPhone == "") {
$("#userPhone-info").html("(required)");
$("#userPhone").css('background-color', '#FFFFDF');
valid = false;
}
if (userEmail == "") {
$("#userEmail-info").html("(required)");
$("#userEmail").css('background-color', '#FFFFDF');
valid = false;
}
if (!userEmail.match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/))
{
$("#userEmail-info").html("(invalid)");
$("#userEmail").css('background-color', '#FFFFDF');
valid = false;
}
if (subject == "") {
$("#subject-info").html("(required)");
$("#subject").css('background-color', '#FFFFDF');
valid = false;
}
if (userMessage == "") {
$("#userMessage-info").html("(required)");
$("#userMessage").css('background-color', '#FFFFDF');
valid = false;
}
return valid;
}
function addMoreAttachment() {
$(".attachment-row:last").clone().insertAfter(".attachment-row:last");
$(".attachment-row:last").find("input").val("");
}
</script>
<body>
<div class="attachment-form-container">
<form name="mailForm" id="mailForm" method="post" action=""
enctype="multipart/form-data" onsubmit="return validate()">
<div class="input-row">
<label style="padding-top: 20px;">Name</label> <span
id="userName-info" class="info"></span><br /> <input
type="text" class="input-field" name="userName"
id="userName" />
</div>
<div class="input-row">
<label>Phone</label> <span id="telefon-info"
class="info"></span><br /> <input type="text"
class="input-field" name="userPhone" id="userPhone" />
</div>
<div class="input-row">
<label>Email</label> <span id="userEmail-info"
class="info"></span><br /> <input type="text"
class="input-field" name="userEmail" id="userEmail" />
</div>
<div class="input-row">
<label>Subject</label> <span id="subject-info"
class="info"></span><br /> <input type="text"
class="input-field" name="subject" id="subject" />
</div>
<div class="input-row">
<label>Message</label> <span id="userMessage-info"
class="info"></span><br />
<textarea name="userMessage" id="userMessage"
class="input-field" id="userMessage" cols="60"
rows="6"></textarea>
</div>
<div class="attachment-row">
<input type="file" class="input-field" name="attachment[]">
</div>
<div onClick="addMoreAttachment();"
class="icon-add-more-attachemnt"
title="Add More Attachments">
<img src="icon-add-more-attachment.png"
alt="Add More Attachments">
</div>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo SITE_KEY ; ?>', {action: 'action_name'}).then(function(token) {
document.getElementById('g-recaptcha-response').value=token;
});
});
</script>
<div>
<input type="submit" name="send" class="btn-submit" value="SEND" />
<div id="statusMessage">
<?php
if (! empty($message)) {
?>
<p class='<?php echo $type; ?>Message'><?php echo $message; ?></p>
<?php
}
?>
</div>
</div>
</form>
</div>
</body>
PHP-код
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(!empty($_POST["send"])) {
require_once ('vendor/autoload.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->Port = 465;
$mail->Username = "************";
$mail->Password = "**********";
$mail->Mailer = "smtp";
$mail->CharSet = "UTF-8";
if (isset($_POST["userEmail"])) {
$userEmail = $_POST["userEmail"];
}
if (isset($_POST["userName"])) {
$userName = $_POST["userName"];
}
if (isset($_POST["userPhone"])) {
$userPhone = $_POST["userPhone"];
}
if (isset($_POST["subject"])) {
$subject = $_POST["subject"];
}
if (isset($_POST["userMessage"])) {
$message = $_POST["userMessage"];
}
$mail->SetFrom("rafal@rafalblacha.pl");
$mail->AddReplyTo($userEmail, $userName);
$mail->AddAddress("admin@rafalblacha.pl");
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($message);
$mail->IsHTML(true);
$mail->SMTPSecure = 'ssl';
$mail->Host = '*************';
$mail->Body =
'<h1 align=centre>Imię i nazwisko: '.$_POST['userName']
.'<br>Telefon: '.$_POST['userPhone']
.'<br>E-mail: '.$_POST['userEmail']
.'<br><br>'.$_POST['userMessage'];
if (! empty($_FILES['attachment'])) {
$count = count($_FILES['attachment']['name']);
if ($count > 0) {
for ($i = 0; $i < $count; $i ++) {
if (! empty($_FILES["attachment"]["name"])) {
$tempFileName = $_FILES["attachment"]["tmp_name"][$i];
$fileName = $_FILES["attachment"]["name"][$i];
$mail->AddAttachment($tempFileName, $fileName);
}
}
}
}
if (! $mail->Send()) {
$message = "Ups :( Error";
$type = "error";
} else {
$message = "Thx ".$_POST['userName']." :)";
$type = "success";
}
}
?>