Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何正确连接reCAPTCHA?_Javascript_Php_Jquery_Html_Recaptcha - Fatal编程技术网

Javascript 如何正确连接reCAPTCHA?

Javascript 如何正确连接reCAPTCHA?,javascript,php,jquery,html,recaptcha,Javascript,Php,Jquery,Html,Recaptcha,得到了这样的联系方式()。 注册验证码。如何在客户机和服务器上实现正确的集成 在表单中,仅插入一个div提交要这样工作吗?如何连接提交和验证码 它指的是POST请求: 它是如何发送的 有一个PHP: <?php // Only process POST reqeusts. if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the form fields and remove whitespace. $name = st

得到了这样的联系方式()。 注册验证码。如何在客户机和服务器上实现正确的集成

在表单中,仅插入一个
div
<代码>提交要这样工作吗?如何连接
提交
和验证码

它指的是POST请求:

它是如何发送的

有一个PHP:

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
    $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    $recipient = "mail@mail.com";

    // Set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

您可以使用此库


首先,在注册站点的密钥

当您的应用程序收到包含g-recaptcha-response字段的表单提交时,您可以使用以下方式进行验证:

<?php
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
    // verified!
} else {
    $errors = $resp->getErrorCodes();
}

我已经在我们的网站上集成了google reCaptcha。这是我们的实现

前端代码:

<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallBack&amp;render=explicit" async defer></script>
<script type="text/javascript">
var recaptcha_sponsorship_signup_form;
var recaptchaCallBack = function() {
    // Render the recaptcha on the element with ID "recaptcha_sponsorship_signup_form"
    recaptcha_sponsorship_signup_form = grecaptcha.render('recaptcha_sponsorship_signup_form', {
        'sitekey' : 'your_recaptcha_website_key',
        'theme' : 'light'
    });
};
</script>

<dt>Prove you’re not a robot</dt>
<dd style="height: 78px;">
<div id="recaptcha_sponsorship_signup_form"></div>                  
</dd>
$fileContent = '';
if (isset($_REQUEST['g-recaptcha-response']) && !empty($_REQUEST['g-recaptcha-response'])) {
    $fileContent = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=your_recaptcha_secret_key&response=". $_REQUEST['g-recaptcha-response']);
}

$jsonArray = json_decode($fileContent, true);
if (isset($jsonArray['success']) && $jsonArray['success']==true) {
    // process your logic here
} else {
    echo "Invalid verification code, please try again!";
}

谢谢似乎对我有用。但也有一些问题。在XAMPP上进行测试。检查“邮件输出”。当我填写表单,但没有检查验证码时,消息没有出现,这很好,但表单刚刚清除,用户没有弹出消息。所以重点是,该用户没有收到任何消息,他错过了验证码。表单在没有验证码的情况下工作,但没有发送。如何修复?如果用户未检查验证码,请添加http\U响应码(403);否则的情况。