1

I have a login form with recaptcha 3 script.

When user type his right details he login in successfully. But if he wrong the recaptcha doesn't allow him to try again and print my error: 'recaptcha_error(2)'

How can i fix it?

HTML - form + recaptcha script

<form id="login-form" action="#" >

    <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
    <input type="hidden" name="action" value="validate_captcha">                        

    <input type="text" name="email">
    <input type="password" name="password">

    <button type="submit">SEND</button>
</form>

<script src="https://www.google.com/recaptcha/api.js?render=MY_SITE_KEY"></script>
<script>
    grecaptcha.ready(function() {
    // do request for recaptcha token
    // response is promise with passed token
        grecaptcha.execute('MY_SITE_KEY', {action:'validate_captcha'})
                  .then(function(token) {
            // add token value to form
            document.getElementById('g-recaptcha-response').value = token;
        });
    });
</script>

My php part (in my ajax file)

if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
} else {
    $captcha = false;
}

if (!$captcha) {

     $msg = 'recaptcha_error(1)';
     
} else {
    $secret   = 'MY_SERVER_KEY';
    $response = file_get_contents(
        "https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']
    );

    $response = json_decode($response);

    
    if ($response->success === false) {

        $msg = 'recaptcha_error(2)';
    }
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
Roi
  • 21
  • 1
  • Are you submitting the form via Ajax? – ADyson Oct 07 '20 at 16:25
  • Yes. does it matter? - @ADyson – Roi Oct 07 '20 at 21:18
  • Yes. Because if you'd done a regular postback, the captcha would be re-loaded along with the rest of the page when it's refreshed after the postback. But if you're using AJAX, that doesn't happen so you'll need some code to trigger a new captcha whenever the response to the AJAX call indicates an invalid captcha. It won't happen automatically / implicitly. – ADyson Oct 07 '20 at 21:20
  • Thanks @ADyson for the quick reply. which part of my code should i trigger again? – Roi Oct 07 '20 at 21:31
  • The bit which initialises the captcha. I haven't had to do this myself on a captcha, but it looks like it would be the `grecaptcha.execute(...` bit. (Unless that JS library has a separate method for refreshing it - if it has any documentation, you can check. Possibly https://stackoverflow.com/questions/3371314/how-to-reload-recaptcha-using-javascript is relevant). – ADyson Oct 07 '20 at 21:42
  • OK, You helped me to figure it out. thanks! – Roi Oct 07 '20 at 22:59
  • No problem. If you got the solution, you should write it in the Answers section, below. You're allowed to answer your own question. Then if others with a similar issue find it useful, you will get upvoted :-) – ADyson Oct 07 '20 at 23:52

0 Answers0