【问题标题】:ReCAPTCHA v3 score issueReCAPTCHA v3 分数问题
【发布时间】:2020-02-22 15:34:07
【问题描述】:

当我尝试使用联系表单编写网站时,我想添加 reCAPTCHA v3 - 但有一些问题.. 我按照本教程进行操作:Tutorial link

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

首先,我在我的 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>

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

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

我了解错误但不知道如何修复它..

请问有人有解决办法吗?

感谢您的帮助:)

杰瑞

【问题讨论】:

    标签: php html undefined recaptcha-v3


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 2023-02-07
      • 2020-08-22
      • 1970-01-01
      • 2022-01-19
      • 2020-01-19
      • 1970-01-01
      相关资源
      最近更新 更多