Error with Google PHP reCaptcha

0

I am placing the reCaptcha in my contact form but every time I send it, the FALSE returns to me:

    $google_url="https://www.google.com/recaptcha/api/siteverify";
    $secret='xxxxxxxx';
    $ip=$_SERVER['REMOTE_ADDR'];
    $url = $google_url."?secret=".$secret."&response=".$recap."&remoteip=".$ip;
    $res = json_decode($url);
    //reCaptcha success check
    if($res['success']){
        echo true;  
    }else{
        echo false;
    }
    
asked by Alf 24.08.2018 в 15:10
source

1 answer

1

Try this code:

1) Declare the script on the page

<script src="https://www.google.com/recaptcha/api.js?hl=es" async defer></script>

2) Make an include

require_once "includes / recaptchalib.php";

3) Valid the captcha

 $secret = "6Le_AyoTA7777777Abv56kY6w_fcPMwEqL";
 $response = null;
 // comprueba la clave secreta
 $reCaptcha = new ReCaptcha($secret);

 if ($_POST["g-recaptcha-response"]) {
     $response = $reCaptcha->verifyResponse(
     $_SERVER["REMOTE_ADDR"],
     $_POST["g-recaptcha-response"]
     );
  }


 if ($response != null &amp;&amp; $response->success) {
    // OK Captcha
  } else {
    // Error Captcha
  }

To create your own secret key access the following link: link

    
answered by 24.08.2018 / 16:48
source