Using reCaptcha always shows "wrong-captcha-sol"

I am trying to use Google reCaptcha on my website and after a few tutorials I am using the following in my HTML page:

<section class="row" id="Contact">
        <div class="col-full">
             <form method="post" action="contact.php">
                <input name="sender" placeholder="Name" size="30"><br><br>
                <input name="senderEmail" placeholder="Email" size="30"><br><br>
                <textarea rows="10" cols="30" name="message" placeholder="Message"></textarea><br><br>
                <div class="g-recaptcha" data-sitekey="6LdFOQcTAAAAABKsku3bD6Mp3HuGYNDRN1tLlSkw"></div><br>
                <input type="submit" name="submit">
            </form>
        </div>
</section>

      

The tag <head>

states the following:

    <script src='https://www.google.com/recaptcha/api.js'></script>

      

The form loads the following PHP script as shown in the Google Tutorial :

<?php

require_once('recaptchalib.php');

echo "<pre> _POST: =========\n";
print_r($_POST);
echo "\n=========\n</pre>";

$privatekey = "privatekey";
$resp = recaptcha_check_answer ($privatekey,
                             $_SERVER["REMOTE_ADDR"],
                             $_POST["recaptcha_challenge_field"],
                             $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
    "(reCAPTCHA said: " . $resp->error . ")");
}

      

? >

The problem is that I always get the error incorrect-captcha-sol

and cannot figure out why. the output I get from the PHP script is:

 _POST: =========
Array
(
 [g-recaptcha-response] => 03AHJ_VusLbTZVbhMBH2-cyOdbM81IoG7ShDtTmyVa8e34PbghyKaRYnrb4bbeuPoJXFa-2sD5B-UIOWq_zClya-VOtq8iLHXBkAR0PrElQYiQjKTm2POkjci43-yEaenh1tu_M4vKsjJGswKFlVs6IdlWhdVaMdznLBeutgHjiHRBy7fzAi0It0x5IadwMxAqVpmG7F1gRvUcQmakij4ObTqPUK4lPdc84HkkKmY62uJ45d8DwKSeR2ifRg6wL9JFxpcZkjSp4oIlltin2uCMiBDU58QQQLLKPJuSKcmm-N5p7OEt7E6gc4UZyTLpKrJ-9mMUusvLpixwFHop0e5155vEZssxzu6Zizo-LyB1mX3XOrQ8LEceydssgLvl9gJBC4f9zDBKPQdLmjfxVKdPCXLtUZVNbMDYe7cNL7gsgwJC-hLbZYTh6UV1nUdKnFnkHAACjI7M28hpKWdiyIwcJ5UAzTMPfIllMw
)

=========

      

Why do I have no fields recaptcha_challenge_field

or recaptcha_response_field

in my POST and how can I solve it?

+3


source to share


3 answers


The code I used for my recaptcha looks like this:

This is the header:

<script src='https://www.google.com/recaptcha/api.js'></script>

      

This means that you want the actual recaptcha to be:



<div class="g-recaptcha" data-sitekey="PUBLIC_KEY_HERE"></div>

      

You can add this to a div to change the theme: data-theme="theme"

And this is the PHP script:

if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];
if(!$captcha){
    //Captcha hasn't been checked
}
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET_KEY_HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false){
    //Captcha incorrect
}
else {
    //Success code here
}

      

+2


source


From what I can see, you have no option else

after checking the answer. You check to see if it is wrong, but there is nothing to tell you what to do if the user got it right. Try adding something like this:



if (!$resp->is_valid) {
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
}
else {
    //Code for success here
}

      

0


source


I am using google new reCaptcha

https://github.com/google/recaptcha

Official code

This is the StandAlone package. It works great with php.

after unsuccessful conversion attempt i use jquery code to reload capctha

grecaptcha.reset();

because recapctha response expires after validation and captcha doesn't reload, so it sends expired response again and again until you reload the page to get new Captcha code.

confirmation code

require('/path/to/recaptcha/autoload.php');

$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
} else { $errors = $resp->getErrorCodes();
foreach ($errors as $code) {
echo "<div class=\"alert alert-danger\">$code</div>";
}
echo "<p><strong>Note:</strong> Error code <tt>missing-input-response</tt> may mean the user just didn't complete the reCAPTCHA.</p>";
}

      

0


source







All Articles