user1833903
user1833903

Reputation: 197

ReCAPTCHA v3 score issue

As I'm trying to code a website with a contact form, I would like to add reCAPTCHA v3 - but have some issues.. I followed this tutorial : Tutorial link

To be more precise, I red other tutorials but thought this one was the most easy to understand.

First of all I created some id's on my google account as localhost and get some keys - next they are indicated as XXXXXXXXXXX.

In my website page are defined php variables and a mail submit method and next, all the HTML part. I tried to simplified it as possible..

Here is the code :

<?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>

But when I use the 'Send' button, it gives me the following error :

Notice: Undefined property: stdClass::$score in /Applications/MAMP/htdocs/Website/main.php on line 13

I understand the error but don't know how to fix it..

Is anyone has a solution please ?

Thank you for your help :)

Jerry

Upvotes: 0

Views: 2138

Answers (1)

Edward68
Edward68

Reputation: 63

I noticed that you were using MAMP. Google does not support testing on local environments. You will notice on the Re-Captcha slide out div that there is a message there that informs you of this. I was having the same issue and as soon as I moved the files across to my domain the error stopped. I hope it works for you.

Upvotes: 1

Related Questions