Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recaptcha missing-input-response

Tags:

php

recaptcha

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

like image 672
Forlis Avatar asked Feb 19 '17 13:02

Forlis


3 Answers

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.

like image 135
vancy-pants Avatar answered Oct 28 '22 12:10

vancy-pants


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
like image 39
Akshay Hegde Avatar answered Oct 28 '22 13:10

Akshay Hegde


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

http_build_query(array(
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $remoteip
), '', '&');
like image 5
Marek Skiba Avatar answered Oct 28 '22 13:10

Marek Skiba