FlashNoob468
FlashNoob468

Reputation: 175

Trouble with reCAPTCHA v3 verify score

Trying to implement a recaptcha on my Contact form for my website and I'm having trouble getting anything to go through unless I set the score to 0.0. Even 0.1 kicks it over to spam. There are so many examples of how to implement, and I've tried several of them but not had any luck (as several are for different versions too, which makes it hard for us noobs).

In any event, here is a stripped down version of the form html page I'm trying to use:

<head>
<script src='https://www.google.com/recaptcha/api.js?render=KEY'></script>
</head>
<body>
<form name="contactform" action="send_form_email.php" method="post">
<div class="input-group">
    <span class="input-group-label">Name</span>
    <input name="realname" class="input-group-field" type="text" value="Your Name Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Email</span>
    <input name="email" class="input-group-field" type="email" value="Your E-Mail Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Message</span>
    <textarea name="message" rows="10"></textarea>
</div>          
<input type="Submit" class="button" value="SEND"><input type="Reset" class="button" value="RESET">
</form>
<script>
    $(function(){ //wait for document ready
        grecaptcha.ready(function() {
            grecaptcha.execute('KEY', {action: 'contactUs'}).then(function(token) {
            // Verify the token on the server.
            });
        });
    });
</script>
</body>

So then I have a PHP form called send_form_email.php that I'm using to take care of all the hard work:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'SECRET_KEY';
$recaptcha_response = $_POST['g-recaptcha-response'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);

    // Take action based on the score returned:
    if ($recaptcha->score >= 0.0) {
    // This is just where I take care of formatting the email and sending it to me, which is working just fine... well while the score is set to 0.0
    }
    } else {
    // otherwise, let the spammer think that they got their message through
    header('Location: success.htm');
    exit();
    }
}
?>

So this is where I run into my issue. In the code above I have it set to 0.0 and that is the ONLY way right now emails come through at all. But of course this lets through spam or real messages because it's basically off. As I said, if I set it to even 0.1 it isn't passing the score check and is never sending the email. I'm sure it's something simple that I'm missing or I'm not passing the information correctly or something, but the google documentation isn't very helpful. So I'm hoping someone can point out what I've missed?

Thanks!

Upvotes: 3

Views: 8778

Answers (1)

FlashNoob468
FlashNoob468

Reputation: 175

Finally found an answer here that gave me exactly what I was looking for. Some simple example code that works! (why can't google do that?) It wasn't listed as the 'accepted' answer, it is the one below that but the accepted answer just tosses you toward a git that is ridiculously confusing for noobs.

Here is my edited my code above from above:

<head>
<script src='https://www.google.com/recaptcha/api.js?render=YOUR_KEY_HERE'></script>
</head>
<body>
<form name="contactform" action="send_form_email.php" method="post">
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
<input type="hidden" name="action" value="validate_captcha">
<div class="input-group">
    <span class="input-group-label">Name</span>
    <input name="realname" class="input-group-field" type="text" value="Your Name Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Email</span>
    <input name="email" class="input-group-field" type="email" value="Your E-Mail Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Message</span>
    <textarea name="message" rows="10"></textarea>
</div>          
<input type="Submit" class="button" value="SEND"><input type="Reset" class="button" value="RESET">
</form>
<script>
    $(function(){ //wait for document ready
        grecaptcha.ready(function() {
            grecaptcha.execute('YOUR_KEY_HERE', {action: 'contactUs'}).then(function(token) {
            // Verify the token on the server.
            document.getElementById('g-recaptcha-response').value = token;
            });
        });
    });
</script>
</body>

Then the revised PHP form called send_form_email.php that I'm using to take care of all the hard work:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'YOUR_SECRET_KEY';
$recaptcha_response = $_POST['g-recaptcha-response'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url.'?secret='.$recaptcha_secret.'&response='.$recaptcha_response);
$recaptcha = json_decode($recaptcha);

    // Take action based on the score returned:
    if ($recaptcha->score >= 0.5) {
    // Basically if the score is equal to or better than the above, you have a good one and can send your email off and this is just where you would do that
    }
    } else {
    // otherwise, let the spammer think that they got their message through
    header('Location: success.htm');
    exit();
    }
}
?>

I've got it showing a 0.5 score for now, but you should of course check your admin on google and see what scores you are getting and adjust as needed.

Upvotes: 3

Related Questions