Forlis
Forlis

Reputation: 237

Recaptcha missing-input-response

I have a problem with google reCaptcha.

Here is my php code:

$secret = 'SECRET_KEY';
            $response = $_POST['g-recaptcha-respone'];
            $remoteip = $_SERVER['REMOTE_ADDR'];

            $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip";
            $result_json = file_get_contents($url);
            $resulting = json_decode($result_json, true);
            print_r($resulting);

if($resulting['success']) {
  //Success
}

input of print_r is: Array ( [success] => [error-codes] => Array ( [0] => missing-input-response ) )

How to solve this problem?

Thanks for answers

Upvotes: 11

Views: 44202

Answers (5)

alen
alen

Reputation: 276

I'm not able to comment so I'm going to answer here. I copied my code which works perfectly and btw $_POST['g-recaptcha-respone'], are you sure your inputs name is g-recaptcha-respone?

$secret = 'SECRET-KEY';
$response = $_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];

$dav = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response."&remoteip=".$ip);

$res = json_decode($dav,true);

if ($res['success']) {
    die(json_encode(0));
} else {
    die(json_encode(1));
}

Upvotes: 3

vancy-pants
vancy-pants

Reputation: 1348

This error happened to me because I had two instances of the ReCaptcha element on my page (one for mobile views, one for desktop). As soon as I removed one of them this error stopped.

Upvotes: 3

Mike
Mike

Reputation: 678

Just a note on this, you should be sending all your params via POST not GET (see https://developers.google.com/recaptcha/docs/verify#api_request). Use something like cURL to help make the request.

Upvotes: 1

Akshay Hegde
Akshay Hegde

Reputation: 16997

Please note : g-recaptcha-respone != g-recaptcha-response

enter image description here

enter image description here

Google reCatcha API you might need to specify additional parameters to the file_get_contents function call, setting the context options specifically for SSL (If site has SSL).

// If submitted check response
if ($_POST["g-recaptcha-response"]) {

// Input data
$secret = 'SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];

$url = "https://www.google.com/recaptcha/api/siteverify";

$post_data = http_build_query(
    array(
        'secret' => $secret,
        'response' => $response,
        'remoteip' => $remoteip
    )
);  

$options=array(

    // If site has SSL then
    'ssl'=>array(

        // In my case its /etc/ssl/certs/cacert.pem

        'cafile'            => '/path/to/cacert.pem',
        'verify_peer'       => true,
        'verify_peer_name'  => true,
    ),

   'http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $post_data
    )
);

$context = stream_context_create( $options );   

$result_json = file_get_contents( $url, false, $context );
$resulting = json_decode($result_json, true);

if($resulting['success']) {
    //Success
} else {
     // action for no response 
}

At least on ubuntu - If site has SSL

cd /usr/local/share/ca-certificates 
sudo curl http://curl.haxx.se/ca/cacert.pem -o cacert.crt 
sudo update-ca-certificates
sudo update-ca-certificates –fresh

and your cafile and path will be

capath=/etc/ssl/certs/
cafile=/etc/ssl/certs/cacert.pem

Upvotes: 15

Marek Skiba
Marek Skiba

Reputation: 2184

In my case I needed to add two extra parameters ('', '&') in this call:

http_build_query(array(
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $remoteip
), '', '&');

Upvotes: 5

Related Questions