User unable to try login again if he type wrong details

55 views Asked by At

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)';
    }
}
0

There are 0 answers