如何正确连接reCAPTCHA?

时间:2015-10-28 12:52:35

标签: javascript php jquery html recaptcha

获得此类联系表单(JSFiddle)。 注册验证码。如何在客户端和服务器上实现正确的集成?

在仅插入div的表单中。 Submit会这样工作吗?如何连接submit和验证码?

它指的是POST请求:

enter image description here

它是如何发送的?

有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.";
}

2 个答案:

答案 0 :(得分:1)

我已将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!";
}

答案 1 :(得分:0)

您可以使用此库;

https://github.com/google/recaptcha/blob/master/examples/example-captcha.php

首先,在https://www.google.com/recaptcha/admin

注册您网站的密钥

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

<strong>Lesson Start Date:</strong> [date-501]

您可以在examples / example-captcha.php

中看到端到端的工作示例