M
M
Maxim Gerasimenko2020-12-06 15:03:50
reCAPTCHA
Maxim Gerasimenko, 2020-12-06 15:03:50

Why is reCAPTCHA v2 not working?

The site has a feedback form. You need to connect Google reCAPTCHA v2 to this form.
Got the secret key and site key on google captcha site. I do according to the manual, but the form is sent regardless of filling in the captcha.
The form code is like this:

<input type="text" name="Name" placeholder="Имя" required><br>
    <input type="text" name="E-mail" placeholder="E-mail" required><br>
    <input type="text" name="Phone" placeholder="Телефон"><br>
    <div class="text-danger" id="recaptchaError"></div>
    <div class="g-recaptcha" data-sitekey="#"></div>
    <button class="button2">Заказать</button>
  </form>

Code for submitting the form and checking captcha in PHP:
<?php
//секретный ключ
$recaptcha = $_POST['g-recaptcha-response'];
if(!empty($recaptcha))
{
$secret='Секретный ключ';
$res=file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$recaptcha);
$res= json_decode($res, true);
//reCaptcha введена
//выполнется код

$method = $_SERVER['REQUEST_METHOD'];

//Script Foreach
$c = true;
if ( $method === 'POST' ) {

  $project_name = trim($_POST["project_name"]);
  $admin_email  = trim($_POST["admin_email"]);
  $form_subject = trim($_POST["form_subject"]);

  foreach ( $_POST as $key => $value ) {
    if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
      $message .= "
      " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
        <td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
        <td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
      </tr>
      ";
    }
  }
} else if ( $method === 'GET' ) {

  $project_name = trim($_GET["project_name"]);
  $admin_email  = trim($_GET["admin_email"]);
  $form_subject = trim($_GET["form_subject"]);

  foreach ( $_GET as $key => $value ) {
    if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
      $message .= "
      " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
        <td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
        <td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
      </tr>
      ";
    }
  }
}

$message = "<table style='width: 100%;'>$message</table>";

function adopt($text) {
  return '=?UTF-8?B?'.Base64_encode($text).'?=';
}

$headers = "MIME-Version: 1.0" . PHP_EOL .
"Content-Type: text/html; charset=utf-8" . PHP_EOL .
'From: '.adopt($project_name).' <'.$admin_email.'>' . PHP_EOL .
'Reply-To: '.$admin_email.'' . PHP_EOL;

mail($admin_email, adopt($form_subject), $message, $headers );

}else{
//reCaptcha не введена
//выполнется код

echo "Вы не заполнили капчу";
}
?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question