1

I am trying to implement Google reCAPTCHA v3 inside a visual studio 2022 PHP project.

For some reason, when I execute the following command to display the values of success and score, I see empty values for '$Return->success' and '$Return->score':

echo '<script type="text/javascript">alert(
    "response->success: ' .  $response->success .
    "\\nresponse->score: " . $response->score . '");</script>';

I have created my own google reCAPTCHA v3 site key and secret key and have inserted them into the source code at the top of the file.

Image of empty '$Return->success' and '$Return->score' values

Bot detected as a result

The following is the source code in the PHP file:

<?php
define('SITE_KEY', '6LcfntEfAAAAAJWKSw4RCluMrbC6NVlwdyFAbwf4');
define('SECRET_KEY', '6LcfntEfAAAAAI6pC8vYRv4Qx8rw2hL7IDXAuK2T');

if($_POST){
    function getCaptcha($SecretKey){
        $Response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".SECRET_KEY."&response={$SecretKey}");
        $Return = json_decode($Response);
        return $Return;
    }
    $Return = getCaptcha($_POST['g-recaptcha-response']);

    echo '<script type="text/javascript">alert(
    "response->success: ' .  $response->success .
    "\\nresponse->score: " . $response->score . '");</script>';

    //var_dump($Return);
    if($Return->success == true && $Return->score > 0.5){
        echo "Success!";
    }else{
        echo "You are a Robot!!";
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>ReCaptcha V3</title>
    <script src='https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY; ?>'></script>
</head>
<body>
    <form action="/" method="POST">
        <input type="text" name="name" /><br />
        <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" /><br />
        <input type="submit" value="Submit" />
    </form>
    <script>
    grecaptcha.ready(function() {
    grecaptcha.execute('<?php echo SITE_KEY; ?>', {action: 'homepage'})
    .then(function(token) {
        //console.log(token);
        document.getElementById('g-recaptcha-response').value=token;
    });
    });
    </script>
</body>
</html>
deniz
  • 3
  • 1
  • 5

1 Answers1

0

Replace if($_POST){ With if(!empty($_POST['g-recaptcha-response'])){

Amir
  • 4,089
  • 4
  • 16
  • 28
  • Hi. I made this change just now, but unfortunately, I am still getting the same result with '$Return->success' and '$Return->score' being empty. – deniz May 08 '22 at 07:31
  • Replace `{$SecretKey}` with `$SecretKey` – Amir May 08 '22 at 08:09
  • Hello. Just made that change, but unfortunately, I am still getting the same result. – deniz May 08 '22 at 15:31