reCaptcha v3 提交表单后没有给我回复?

问题描述 投票:0回答:1

我正在尝试为我的联系页面实现 reCaptcha v3,但 reCaptcha 不配合。我对 reCaptcha 完全陌生。我尝试按照文档的示例来实现,并尝试一路“注意与 ChatGPT 的差距”,但我仍然陷入困境。我似乎没有收到 reCaptcha 的回复:

我不确定我在这里做错了什么。在有人问之前,是的,我确实有钥匙,只是出于安全考虑而没有将它们放在这里。是的,我确实为我的本地主机设置了密钥(我现在正在使用它)。

代码:

<?php include "head.php" ?>
    <title>How to Vote 2024 | Contact </title>
    <script src="https://www.google.com/recaptcha/api.js"></script>
    <script>
        function onSubmit(token) {
            document.getElementById("form").submit();
        }
    </script>

</head>

<?php include "header.php"?>
<main>
    <div class="container">
        <h1>Contact</h1>

        <p>
            If you want to bring a bug to my attention or perhaps you're a party rep that wants to add information to your party's page or even recommend a party to be added or
            maybe just give a compliment. Please contact me using the form below.
        </p>

        <form id="form" action="../model/emails.php" method="post">
            <input type="hidden" name="recaptchaResponse" id="recaptchaResponse">
            <div class="mb-3">
                <label for="email" class="form-label">Your Email Address:</label>
                <input id="email" name="email" type="email" class="form-control" placeholder="[email protected]" maxlength="25"/>
            </div>

            <div class="mb-3">
                <label for="name" class="form-label">Your Name:</label>
                <input id="name" name="name" type="text" class="form-control" placeholder="John Matthews" maxlength="25"/>
            </div>

            <div class="mb-3">
                <label for="name" class="form-label">Your Message:</label>
                <textarea id="message" name="message" class="form-control" placeholder="your message here..." maxlength="255"></textarea>
            </div>

            <input type="hidden" name="action" value="validate_captcha">

            <button type="submit"
                data-sitekey="" 
                data-callback='onSubmit' 
                data-action='submit' class="btn btn-primary g-recaptcha">Send Message</button>
                <p class="text-center">
                    <small>This form is protected by reCAPTCHA and the Google <a href="https://policies.google.com/privacy">Privacy Policy</a> and <a href="https://policies.google.com/terms">Terms of Service</a> apply.</small>
            </p>
        </form>
    </div>

</main>

<?php include "footer.php" ?>
<?php 
    // Handles the email workings from the contact form on the Contact page
    $errors = [];
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Get and clean POST data
        $name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '';
        $email = isset($_POST['email']) ? $_POST['email'] : '';
        $message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '';
        
        $email = filter_var($email, FILTER_SANITIZE_EMAIL);
        $email = filter_var($email, FILTER_VALIDATE_EMAIL);
        
        // Validate form fields
        if (empty($name)) {
            $errors[] = 'Name is empty';
        }
        
        if (empty($email)) {
            $errors[] = 'Email is empty or invalid';
        }
        
        if (empty($message)) {
            $errors[] = 'Message is empty';
        }        
    
        // If no errors, send email
        if (empty($errors)) {
            // Recipient email address (replace with your own)
            $recipient = "[email protected]";
    
            // Additional headers
            $headers = "From: $name <$email>";

            $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'; // URL to the reCAPTCHA server
            $recaptcha_secret = ''; // Secret key
            $recaptcha_response = $_POST['recaptchaResponse']; // Response from reCAPTCHA server, added to the form during processing
            echo '&response='.$recaptcha_response;
            $recaptcha = file_get_contents($recaptcha_url.'?secret='.$recaptcha_secret.'&response='.$recaptcha_response); // Send request to the server
            $recaptcha = json_decode($recaptcha); // Decode the JSON response
            if($recaptcha->success == true && $recaptcha->score >= 0.5 && $recaptcha->action == "submit"){ // If the response is valid
                // run email send routine
                echo $recaptcha->success;
                echo $recaptcha->score;
                echo $recaptcha->action;
                mail($recipient, 'Contact Form Submission', $message, $headers);
                echo 'Your message was sent successfully.'; // Success message
            }else{
                echo $recaptcha->success;
                echo $recaptcha->score;
                echo $recaptcha->action;
                echo 'Something went wrong. Please try again later'; // Error message
            }
        } else {
            // Display errors
            echo "The form contains the following errors:<br>";
            foreach ($errors as $error) {
                echo "- $error<br>";
            }
        }
    } else {
        // Not a POST request, display a 403 forbidden error
        header("HTTP/1.1 403 Forbidden");
        echo "You are not allowed to access this page.";
    }
?>

有问题的页面:

php recaptcha
1个回答
0
投票

问题似乎可能与您处理 reCAPTCHA 响应的方式有关。您收到的错误消息

(Undefined property stdClass: $score and Undefined property stdClass: $action)
表明属性分数和操作在响应对象中不存在或不可访问。

reCAPTCHA 响应应包含以下属性:success 表示验证是否成功;score 表示用户的置信度;action 表示用户执行的操作。

如果有效,请尝试以下代码,否则您需要调试他们的响应,

$recaptcha_response = $_POST['recaptchaResponse'];
$recaptcha_secret = 'YOUR_RECAPTCHA_SECRET_KEY'; // Replace with your actual secret key
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_data = [
    'secret' => $recaptcha_secret,
    'response' => $recaptcha_response
];

$recaptcha_options = [
    'http' => [
        'method' => 'POST',
        'content' => http_build_query($recaptcha_data)
    ]
];
$recaptcha_context = stream_context_create($recaptcha_options);
$recaptcha_result = file_get_contents($recaptcha_url, false, $recaptcha_context);
$recaptcha_data = json_decode($recaptcha_result);
if ($recaptcha_data->success && $recaptcha_data->score >= 0.5 && $recaptcha_data->action === 'submit') {
    // Your code to send the email
    mail($recipient, 'Contact Form Submission', $message, $headers);
    echo 'Your message was sent successfully.';
} else {
    echo 'Something went wrong. Please try again later.';
}
© www.soinside.com 2019 - 2024. All rights reserved.