Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase ReCaptcha expiration time?

How to increase the google ReCaptcha expiration time? I have tried below one,

  <script>
   var callback = function() {
      grecaptcha.render('id-of-render-element', {
         'sitekey': 'your-site-key',
         'expired-callback': expCallback
       });
   };
   var expCallback = function() {
      grecaptcha.reset();
   };
</script>
    <div id="id-of-render-element"></div>
<script src="https://www.google.com/recaptcha/api.js?onload=callback&render=explicit" async defer></script>

But it is not working.

like image 655
Renu Avatar asked Jun 29 '17 09:06

Renu


2 Answers

This has worked for me on both Invisible and the Checkbox versions. I understand you want to "increase" the time, however that is not an option Google offers. I simply reboot the box every 5 min.

setInterval(function(){ grecaptcha.reset(); }, 5 * 60 * 1000 ); 
// just reset every 5 min

Also, setting the "Advanced" box slider in your Google Developer Console to "least restrictions" seems to help.

Keep in mind with the new "Invisible" Recapcha you may not need to reset or extend the time unless you have additional AJAX validation. I have noticed that when I use the new "invisible" method that the box stays good forever if I only trigger it when the form is validated first.

Q.E.D. A better solution to the problem might then be simple to use this https://developers.google.com/recaptcha/docs/invisible#auto_render

like image 133
Christian Žagarskas Avatar answered Oct 11 '22 06:10

Christian Žagarskas


Best Solution

<input type='hidden' name='recaptcha_token' id='recaptcha_token'>

<script src="https://www.google.com/recaptcha/api.js?render={{ env("GOOGLE_CAPTCHA_PUBLIC_KEY") }}"></script>

<script>
    setInterval(function () { 
        grecaptcha.ready(function() {
            grecaptcha.execute('{{ env("GOOGLE_CAPTCHA_PUBLIC_KEY") }}')    
            .then(function(token) {
                document.getElementById("recaptcha_token").value = token;
            }); 
        });
    }, 30 * 1000);
</script>
like image 39
Ahkmy990 Avatar answered Oct 11 '22 05:10

Ahkmy990