D
D
Danil Solovyov2018-07-12 20:12:08
PHP
Danil Solovyov, 2018-07-12 20:12:08

What am I doing wrong with Google reCaptcha v3?

The crux of the issue is that the Google API returns error: 0 missing-input-response after submitting the form.
I read integration info here: https://developers.google.com/recaptcha/docs/v3
But I didn't understand anything

HTML
<div class="modal fade" id="contactForm" tabindex="-1" role="dialog" aria-labelledby="contactFormLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="contactFormLabel">Связаться со мной</h4>
                    </div>
                    <div class="modal-body">
                        <form method="POST" action="../send.php" id="send-form" class="form-horizontal" role="form">
                            <div class="form-group">
                                <label for="inputName" class="col-sm-2 control-label">Имя</label>
                                <div class="col-sm-10">
                                    <input type="text" name="name" class="form-control" id="inputName" placeholder="Имя" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="inputEmail" class="col-sm-2 control-label">Email</label>
                                <div class="col-sm-10">
                                    <input type="email" name="email" class="form-control" id="inputEmail" placeholder="Email" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="inputTel" class="col-sm-2 control-label">Телефон</label>
                                <div class="col-sm-10">
                                    <input type="tel" name="tel" class="form-control" id="inputTel" placeholder="Телефон" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="inputProblem" class="col-sm-2 control-label">Опишите вашу задачу</label>
                                <div class="col-sm-10">
                                    <textarea name="problem" class="form-control" id="inputProblem" cols="30" rows="10" placeholder="Опишите вашу задачу" required></textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Закрыть</button>
                        <button type="submit" class="btn btn-primary" id="send-form-submit" form="send-form" data-loading-text="Отправка...">Отправить</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>

JS/JQUERY
//Send Form
    $('#send-form').submit(function (e) {
        e.preventDefault();
        var form = $(this);
        var btn = $('#send-form-submit');
        btn.button('loading');
        var m_action = form.attr('action');
        var name = form.find('#inputName').val();
        var email = form.find('#inputEmail').val();
        var tel = form.find('#inputTel').val();
        var problem = form.find('#inputProblem').val();
        var from = 'не удалось определить.';

        grecaptcha.ready(function () {
            grecaptcha.execute('мой публичный ключ', {
                    action: 'sendform'
                })
                .then(function (token) {
                    if (document.referrer != "") {
                        from = document.referrer;
                    }
                    $.post(
                        m_action, {
                            name: name,
                            email: email,
                            tel: tel,
                            problem: problem,
                            from: from,
                            token: token
                        },
                        function (data) {
                            alertMessage(data);
                            btn.button('reset');
                        }
                    );
                });
        });
    });

PHP
<?php
if ($_POST['name']) {
    $url_google_api = 'https://www.google.com/recaptcha/api/siteverify';
    $secret = 'мой секретный ключ';
    // Recaptcha V3
    $query = $url_google_api . '?secret=' . $secret . '&response=' . $POST['token'] . '&remoteip=' . $_SERVER['REMOTE_ADDR'];
    $data = json_decode(file_get_contents($query));
    $success = 'false';
    if ($data->success) {
        $success = 'true';
    }

    $message = "Имя пользователя: " . htmlspecialchars($_POST['name']) . "<br/>";
    $message .= "Телефон пользователя: " . htmlspecialchars($_POST['tel']) . "<br/>";
    $message .= "E-mail пользователя: " . htmlspecialchars($_POST['email']) . "<br/>";
    $message .= "Задача пользователя: " . htmlspecialchars($_POST['problem']) . "<br/>";
    $message .= "Переход осуществлен с: " . htmlspecialchars($_POST['from']) . "<br/>";

    $message .= "Data Google API:<br/>";
    foreach ($data as $k => $v) {
        $message .= "{$k} - {$v}<br/>";
    }

    $message .= "Data Google API ERRORS:<br/>";
    foreach ($data->{'error-codes'} as $k => $v) {
        $message .= "{$k} - {$v}<br/>";
    }
    send_mail($message);
}

function send_mail($message) {
    $mail_to = "[email protected]"; // почта, на которую придет письмо
    $subject = "Форма обратной связи (Новое сообщение): " . $_POST['name'] . " - " . $_POST['tel']; // тема письма
    // заголовок письма
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n"; // кодировка письма
    $headers .= "From: Веб-сайт test.com <[email protected]>\r\n"; // от кого письмо
    if (mail($mail_to, $subject, $message, $headers)) {
        echo json_encode(true);
    }
    else {
        echo json_encode(false);
    }
}
?>

Thanks in advance for your replies :).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Solovyov, 2018-07-12
@SolovyevDanil

Issue resolved, due to inattention. Typo in php code. Instead it is necessary . $POST['token']$_POST['token']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question