reCAPTCHA v3 verification fails with timeout-or-duplicate
timeout-or-duplicateShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
error-codes: timeout-or-duplicate, mostly on long formsThe token is older than two minutesFix - 2
error-codes: timeout-or-duplicate right after a successful callThe same token is verified twiceFix - 3
error-codes: invalid-input-secret, invalid-input-response or missing-input-responseThe secret or the response field is wrongFix
- 1
What does the error look like?
timeout-or-duplicateinvalid-input-responsemissing-input-responseinvalid-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/null2Fix
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
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 post3Verify
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
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix