-1

I'm trying to implement Google ReCaptcha V2 in a PHP form.

Here is my code :

    $arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);

if($_SERVER["REQUEST_METHOD"] === "POST")
    {
        //form submitted

        //check if other form details are correct

        //verify captcha
        $recaptcha_secret = "";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'], false, stream_context_create($arrContextOptions));
        $response = json_decode($response, true);
        if($response["success"] === true)
        {
            echo "Logged In Successfully";
        }
        else
        {
            echo "You are a robot";
        }
    }

?>

When i submit my form, it always return

You are a robot

.

My public key is correct, and my private key too.

I don't know what i'm doing wrong ?

I'm working as localhost.

Thanks.

Tweakdev
  • 113
  • 1
  • 1
  • 8

1 Answers1

1

Just integrated 2 days ago the V2 recaptcha from Google

Try my code below, explicitly to see if is solving your problem:

I can see u do file_get_contents, and i think here is your issues, u have to make POST, please use my code below

if($_SERVER["REQUEST_METHOD"] === "POST"){
    // prepare post variables
    $post = [
        'secret' => $secret,
        'response' => $_POST['g-recaptcha-response'],
        'remoteip'   => 'is optional, but i pass it',
    ];

    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $response = curl_exec($ch);
    curl_close($ch);

    var_dump($response);
    $response = json_decode($response, true);

    // check result
    if(isset($response['success']) && $response['success'] == true){
        echo "Logged In Successfully";
    }else{
        echo "You are a robot";
    }
}
aaa
  • 446
  • 2
  • 8
  • 29
  • Hi, thanks for your answer. When i'm trying with your code, it says "string(75) "{ "success": false, "error-codes": [ "missing-input-response" ] }" You are a robot " – Tweakdev May 25 '18 at 10:34
  • make var_dump($_POST['g-recaptcha-response']); what u get? do u use ajax for your form? seems $_POST['g-recaptcha-response'] is not returning the key – aaa May 25 '18 at 10:35
  • @Tweakdev be sure that $_POST['g-recaptcha-response'] hold the capcta response, i guess is returning empty string now from your form – aaa May 25 '18 at 10:37
  • var_dump($_POST['g-recaptcha-response']); displays "NULL" – Tweakdev May 25 '18 at 12:02
  • @Tweakdev because of this u have issue, the response is not in the formdata, your form is simple php form with refresh or u use ajax? – aaa May 25 '18 at 12:06
  • my form is in HTML with
    , maybe i have to do a PHP Form ?
    – Tweakdev May 25 '18 at 12:08
  • @Tweakdev can u put all the code with the form and all php, to test it? – aaa May 25 '18 at 12:10
  • i put my HTML form as an answer, the PHP is as top of the page. – Tweakdev May 25 '18 at 12:19
  • I found the solution @ionluchian , it was a problem of
    . I had
    and
    after, but i have to put the
    in the , thanks for your time, you are a good guy
    – Tweakdev May 25 '18 at 12:23