Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

reCAPTCHA v3 verification fails with timeout-or-duplicate

Errortimeout-or-duplicate

Short answer

The siteverify call returns timeout-or-duplicate when the token is more than two minutes old or was already verified once. Request a new token with grecaptcha.execute when the user submits, send it once, and never verify the same token twice. Other error codes point to the secret or the token field.

  • Applies to reCAPTCHA v3, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the error-codes array that Google sends back. The code tells you which cause below applies.

    $reply = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false,
      stream_context_create(['http' => [
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => http_build_query(['secret' => $secret, 'response' => $_POST['g-recaptcha-response'] ?? '']),
      ]]));
    error_log($reply);
  2. 2Match what you see, then jump to its fix

    1. 1error-codes: timeout-or-duplicate, mostly on long formsThe token is older than two minutesFix
    2. 2error-codes: timeout-or-duplicate right after a successful callThe same token is verified twiceFix
    3. 3error-codes: invalid-input-secret, invalid-input-response or missing-input-responseThe secret or the response field is wrongFix

What does the error look like?

  • timeout-or-duplicate
  • invalid-input-response
  • missing-input-response
  • invalid-input-secret

Cause 1 of 3

The token is older than two minutes

1Check

Each token is valid for two minutes. If the script asks for the token when the page loads, a user who fills the form slowly sends an expired one.

grep -n "grecaptcha.execute" -r public/ resources/ 2>/dev/null

2Fix

Call grecaptcha.execute inside the submit handler, so the token is made at the moment of the action.

form.addEventListener('submit', (e) => {
  e.preventDefault();
  grecaptcha.ready(() => {
    grecaptcha.execute('SITE_KEY', { action: 'submit' }).then((token) => {
      form.querySelector('[name=g-recaptcha-response]').value = token;
      form.submit();
    });
  });
});

3Verify

Wait three minutes on the form, submit, and check that verification still passes.

var_dump(json_decode($reply, true)['error-codes'] ?? []);

Confirmed by developers.google.com

Did this fix it?

Cause 2 of 3

The same token is verified twice

1Check

A token can be verified only once. Look for a second siteverify call on the same request, a retry after a timeout, or a form that is resubmitted with the old token.

grep -rn "siteverify" --include=*.php .

2Fix

Verify once per submit, keep the result, and on a failed form ask the browser for a new token before the user submits again.

// after a failed submit, run grecaptcha.execute again
// and replace the hidden field before the next post

3Verify

Submit twice in a row; each post carries its own token and both are checked once.

var_dump(json_decode($reply, true)['error-codes'] ?? []);

Confirmed by developers.google.com

Did this fix it?

Cause 3 of 3

The secret or the response field is wrong

1Check

invalid-input-secret means the secret is malformed or wrong, and missing-input-response means the response parameter was not sent. Compare the field name your PHP reads with the one your form posts, and check you used the secret key, not the site key.

php -r 'var_dump(getenv("RECAPTCHA_SECRET") !== false);'

2Fix

Send the secret and the token as POST parameters named secret and response, and read the token from the field your page really posts.

http_build_query([
  'secret'   => getenv('RECAPTCHA_SECRET'),
  'response' => $_POST['g-recaptcha-response'] ?? '',
]);

3Verify

The error-codes array is empty and success is true.

var_dump(json_decode($reply, true)['error-codes'] ?? []);

Confirmed by developers.google.com

Did this fix it?

How do I stop it coming back?

  • Generate the token on submit, not on page load.
  • Log error-codes from every failed verification.
  • Check the action name Google returns against the action you expect.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.