PHP - Validate the recaptcha v2 response

12,546

Remove this line:

$captcha = $_GET["g-recaptcha-response"];

Then you need to decode the json by Google like so:

$g_response = json_decode($response);

Then just check with if/else:

if ($g_response->success === true) echo "success!";
Share:
12,546
baconck
Author by

baconck

Updated on June 24, 2022

Comments

  • baconck
    baconck almost 2 years
    #form.php
    
    if(isset($_POST['g-recaptcha-response'])){
        $captcha=$_POST['g-recaptcha-response'];
        $captcha=$_GET["g-recaptcha-response"];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=__1234__&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
    
        echo $response;
    

    Returns:

    Notice: Undefined index: g-recaptcha-response in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 114 { "success": false, "error-codes": [ "missing-input-response" ] }

    echo $response->success;
    

    Returns:

    Notice: Trying to get property of non-object in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 119

    I just need to get the "success" object, then I can test if it is True OR False and I am done.

    • joyBlanks
      joyBlanks over 8 years
      try var_dump($_POST) check if the key value you are seeking exists
    • baconck
      baconck over 8 years
      echo $response; returns { "success": false, "error-codes": [ "missing-input-response" ] } so the string is empty making the response false. I need to get the "success" value.
    • Admin
      Admin over 8 years
      this is not the defult provided by google php libairy, have you tried that
    • baconck
      baconck over 8 years
      recaptcha v2 does not provide a php lib that I know about...???
    • devman
      devman over 5 years
      you shouldn't post your secret on the internet btw
    • vandijkstef
      vandijkstef about 5 years
      Please note that you've included your secret key in the question. Please don't do that again