不论Recaptcha为何,都将提交联系表-怀疑ajax验证程序

时间:2019-08-30 12:50:44

标签: php ajax validation submit recaptcha

我已经为一个有效的网站制作了联系表格-如此好,以至于僵尸程序使用该网站向公司发送垃圾邮件。不好。

我已经考虑添加一个Google Recaptcha验证系统。

我已经成功地将该小部件添加到了网站,并且扩展到:

  • 如果验证码在单击提交时未得到验证,则网站会向用户返回错误。
  • 验证验证码并按提交后,网站会显示正确的确认消息。

到目前为止,一切都很好。

问题:

无论验证码如何,表单都会发送。因此,无论它是否确实向用户显示有关验证码验证的错误,它仍会提交消息-最终使该机器人停止重新验证毫无价值。看起来不错...但是真的吗?还没有。

我怀疑是由我执行的.js验证脚本。我已经尝试过一些事情,例如更改代码的顺序等等,但是我的新手身份确实让我很难受,并且我快要放弃了。

我的表单使用php,ajax进行即时验证(如果发送了表单),js以及很好的旧html和css。

.PHP:     

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$firstname = ($_POST['firstname']);
$lastname = ($_POST['lastname']);
$phone = ($_POST['phone']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$msg = ($_POST['msg']);
$okMessage = ' SUCCESS ';
$errorMessage = ' ERROR';

$secretKey = " EXAMPLE ";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];

$mail = new PHPMailer(true);
try {
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Host       = ' EXAMPLE ';
    $mail->SMTPAuth   = true;
    $mail->Username   = ' EXAMPLE ';
    $mail->Password   = ' EXAMPLE ';
    $mail->SMTPSecure = 'ssl';
    $mail->Port       = 465;
    $mail->WordWrap = 50;
    $mail->Priority = 1;
    $mail->CharSet = 'utf-8';

    $mail->setFrom(' EXAMPLE ', ' EXAMPLE ');
    $mail->addAddress(' EXAMPLE ');
    $mail->addReplyTo($email);
    $mail->isHTML(true);

    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($response);

    $mail->Subject = 'NY BESKED -  EXAMPLE';
    $mail->Body    = (' EXAMPLE ');
    $mail->AltBody = (' EXAMPLE ');

    if (!$mail->send() || !$response->success) {
        throw new \Exception('ERROR TRY AGAIN' . $mail->ErrorInfo);
    } else  {
        $responseArray = array('type' => 'success', 'message' => $okMessage);
    }

} catch (\Exception $e) {
    $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);
    header('Content-Type: application/json');
    echo $encoded;
} else {
    echo $responseArray['message'];
}

JS PART:

$(function ValidateEmailForm() {
    window.verifyRecaptchaCallback = function (response) {
        $('input[data-recaptcha]').val(response).trigger('change')
    };
    window.expiredRecaptchaCallback = function () {
        $('input[data-recaptcha]').val("").trigger('change')
    };
    $('#contact-form').validator();
    $('#contact-form').on('submit', function (e) {
        if (!e.isDefaultPrevented()) {
            var url = "contact.php";

            $.ajax({
                type: "POST",
                url: url,
                data: $(this).serialize(),
                success: function (data) {
                    var messageAlert = 'alert-' + data.type;
                    var messageText = data.message;

                    var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                    if (messageAlert && messageText) {
                        $('#contact-form').find('.messages').html(alertBox);
                        $('#contact-form')[0].reset();
                        grecaptcha.reset();
                    }
                }
            });
            return false;
        }
    });
});

和HTML部分:

          <script src="https://www.google.com/recaptcha/api.js" async defer></script>

          <div class="col-12 d-flex justify-content-center">
            <div class="g-recaptcha" data-sitekey="6Lf80bUUAAAAADrnadBM_GYs0PY8p4QqP7ol45ac"></div>
          </div>

这是什么问题?预先谢谢你!

1 个答案:

答案 0 :(得分:1)

您的条件落后。您应该先检查验证码:

firebase deploy