0

I am trying to implement google reCAPTCHA. The issue is even when ($Return->success == true && $Return->score >= 0.5) is equal to true, when i should be getting $isOK = true, I get $isOK = false.

I tried a lot, but I am still getting this value set to false. Is there something I am doing wrong over here?

$captchaVerified = false;
if(isset($_POST['tokenVal'])){
    function getCaptcha($tokenV,$secretKey){
        $Response =  file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".urlencode($secretKey)."&response=".urlencode($tokenV));
        $Return = json_decode($Response);
        /* global $captchaVerified;*/
        if ($Return->success == true && $Return->score >= 0.5) {
            $captchaVerified = true;
        } else {
            $captchaVerified = false;
        }
        return $captchaVerified;
    }
   $isOK = getCaptcha($_POST['tokenVal'],$secretKey);
}
xKobalt
  • 1,498
  • 2
  • 13
  • 19
A.J
  • 1
  • 1
  • 2
    How do you check the value? DO you output it? Where and how? – u_mulder Jul 24 '20 at 07:34
  • I output it using $isOk and can see in debugger that it sets to true when it meet the condition ($Return->success == true && $Return->score >= 0.5). But after being set to true its gain set to false when it exits the function. – A.J Jul 24 '20 at 07:57
  • That's impossible. How do you know it "meets the condition"? Also, are you testing `$isOK` after the function, or `$captchaVerified`? There is no reason for the latter to be defined outside the function, and it'll remain `false` there. – Jeto Jul 24 '20 at 08:02
  • 1
    I found the issue. I was mistakenly checking the value $captchaVerified which was being reset to its global value. @Jeto you were right. – A.J Jul 27 '20 at 02:55

1 Answers1

0

The given Response code is performing a GET request to the api server.

The api server is expecting a POST request.

$Response =  file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".urlencode($secretKey)."&response=".urlencode($tokenV));

To make POST request to the server you can follow these steps .

The overall reference has been taken from verify recaptcha