首页 > 解决方案 > ReCAPTCHA v3 分数问题

问题描述

当我尝试使用联系表格编写网站时,我想添加 reCAPTCHA v3 - 但有一些问题.. 我遵循了本教程:教程链接

更准确地说,我红了其他教程,但认为这个是最容易理解的。

首先,我在我的 google 帐户上创建了一些 id 作为 localhost 并获取了一些密钥 - 接下来它们被指示为 XXXXXXXXXXX。

在我的网站页面中定义了 php 变量和邮件提交方法,接下来是所有 HTML 部分。我试图尽可能简化它..

这是代码:

<?php 
    define('SITE_KEY', 'XXXXXXXXXXX');    
    define('SECRET_KEY', 'XXXXXXXXXXX');

    if ($_SERVER['REQUEST_METHOD'] === 'POST' AND isset($_POST['recaptcha_response'])) 
    {         
        $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
        $recaptcha_secret = SECRET_KEY;
        $recaptcha_response = $_POST['recaptcha_response'];
        $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
        $recaptcha = json_decode($recaptcha);
        if ($recaptcha->score >= 0.5)
        {  
            if (isset($_POST['send_message']))
            {
                include("mail_contact.php");   //method to send email
            }
        }
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="Pages/style.css" />
        <title>Contact page</title>
        <script src='https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY; ?>'></script>
        <script>
            grecaptcha.ready(function()
            {
                grecaptcha.execute(<?php echo SITE_KEY; ?>, {action: 'send_message'}).then(function(token)
                {
                    document.getElementById('recaptchaResponse').value = token;
                });
            });
        </script>
    </head>
    <body class="preload">
            <header>
                <?php include("Pages/header.php"); ?>
            </header>

            <section>
                        <form method="post" action="">
                            <div class="grille">
                                <div class="demibox">
                                    <label for="last_name">Nom :</label>
                                    <input type="text" name="last_name" id="last_name" maxlength="20" required />
                                </div>
                                <div class="demibox">
                                    <label class="label_name" for="first_name">Prenom :</label>
                                    <input type="text" name="first_name" id="first_name" maxlength="20" required />
                                </div>
                            </div>
                            <div class="grille">
                                <label for="email">E-mail :</label>
                                <input type="email" name="email" id="email" maxlength="55" required />
                            </div>
                            <div class="grille_message">
                                <textarea name="message" id="message" required ></textarea> 
                            </div>
                            <button name="send_message">
                                <span>Envoyer</span>
                            </button>
                            <input type="hidden" name="recaptcha_response" id="recaptchaResponse">
                        </form>
            </section>

            <footer>
                <?php include("Pages/footer.php"); ?>
            </footer>
        </div>
    </body>
</html>

但是当我使用“发送”按钮时,它给了我以下错误:

注意:未定义的属性:第 13 行 /Applications/MAMP/htdocs/Website/main.php 中的 stdClass::$score

我理解错误但不知道如何解决它..

请问有人有解决方案吗?

谢谢您的帮助 :)

杰瑞

标签: phphtmlundefinedrecaptcha-v3

解决方案


我注意到您正在使用 MAMP。Google 不支持在本地环境中进行测试。您会在 Re-Captcha 滑出 div 上注意到,那里有一条消息通知您这一点。我遇到了同样的问题,一旦我将文件移动到我的域中,错误就停止了。我希望这个对你有用。


推荐阅读