L
L
Lucker_Kophe2020-11-20 21:49:00
PHP
Lucker_Kophe, 2020-11-20 21:49:00

Why is ReCaptcha not working?

Hello, please tell me why ReCaptcha may not work.
JS:

form.addEventListener('submit', subTo);

async function subTo(e) {
    e.preventDefault();
    let tk = '';

    grecaptcha.ready(function() {
        grecaptcha.execute('SecretKey', {action: 'homepage'}).then(function(token) {
            tk = token;
            document.getElementById('token').value = token;

            const data = new URLSearchParams();
          for (const pair of new FormData(document.querySelector('form'))) {
            data.append(pair[0], pair[1]);
          }
          fetch('action.php', {
            method: 'post',
            body: data,
          })
            .then(response => response.json())
            .then(result => {
                if (result['om_score'] >= 0.5) {
                    console.log('Человек')
                    // отправка данных на почту
                    if (error_time == 0) {
                        submit.className += ' okay-form'; submit.disabled='disabled'; submit.innerHTML='Отправлено';
                        $name.className += ' okay-form';
                        $email.className += ' okay-form';
                        $phone.className += ' okay-form';
                        $message.className += ' okay-form'; $message.disabled='disabled';
                        setTimeout(function sub(){document.location.reload();}, 2000);
                   }               
                } else {
                    console.log('Бот')
                }
            });
        });
      });
}


PHP:

$captcha;
if (isset($_POST['token'])) {
    $captcha = $_POST['token'];
}

$secretKey = 'SecretKey';
$ip = $_SERVER['REMOVE_ADDR'];

$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secretKey . '&response=' . $captcha;

$response = file_get_contents($url);
$responseKeys = json_decode($response, true);

header('Content-type: application/json');

if($responseKeys["success"] && $responseKeys["score"] >= 0.5) {
  echo json_encode(array('success' => 'true', 'om_score' => $responseKeys["score"], 'token' => $_POST['token']));
} else {
  echo json_encode(array('success' => 'false', 'om_score' => $responseKeys["score"], 'token' => $_POST['token']));
}

$subject = "ГЛАВНАЯ";

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message']; 

 require 'phpmailer/PHPMailer.php';
 require 'phpmailer/SMTP.php';
 require 'phpmailer/Exception.php';

require 'ShablonAction.php';


HTML form:

<form class="text-forms" method="POST" action=''>
    <input type="text" placeholder="Имя" name="name" class="input_name" id='name'><br>
    <input type="text" placeholder="Email" name="email" class="input_email" id='email'><br>
    <input type="text" placeholder="Tel" name="phone" class="input_phone" id='phone'><br>
    <textarea placeholder="Сообщение" name="message" class="text_area-mes" id='message'></textarea><br>
    <input type="hidden" id="token" name="token">
    <button type="submit" class="form_submit">Отправить</button>
</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-11-20
@Lucker_Kophe

What exactly doesn't work?


grecaptcha.execute('SecretKey'...
$url = ' https://www.google.com/recaptcha/api/siteverify?secret= ' . $secretKey ....

Here, if anything, two different keys are used (site key and secret key).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question