Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google recaptcha not validating on server side the second time

Tags:

php

recaptcha

I have a form with multiple fields & recaptcha. If one of the fields is invalid when submit button is clicked & recaptcha is valid it submits the form but validates those fields and returns an error message to the user if it failed.

At this stage recaptcha remains checked & validated on frontend. If user fixes the issue on the field that had the error message and resubmits the form the response from recaptcha is false and therefore not validated.

enter image description here

After I enter valid email, I get this:

enter image description here

My function that checks the captcha is this:

enter image description here

on false it results in that error message that is displayed on above screenshot.

like image 601
GGio Avatar asked Oct 16 '25 14:10

GGio


2 Answers

A recaptcha validation is good only one time.

You have a couple of possible approaches:

  • Save a session variable that the user has successfully completed the captcha and don't require it to pass validation the second time. Be sure to clear the variable on successful submission, or an attacker could solve once and submit a million times.
  • Redisplay the captcha (using a grecaptcha.render call) and require them to fill it out again if the form submission fails.
  • Don't validate the captcha until all other fields validate first.
like image 53
ceejayoz Avatar answered Oct 19 '25 07:10

ceejayoz


From the official docs:

If your website performs server side validation using an AJAX request, you should only verify the user’s reCAPTCHA response token (g-recaptcha-response) once. If a verify attempt has been made with a particular token, it cannot be used again. You will need to call grecaptcha.reset() to ask the end user to verify with reCAPTCHA again.

Which means, your verifyCaptcha() function must only run once. Save a verification flag into a session var and simply check if it exists and is valid on every subsequent request.

like image 36
Dan H Avatar answered Oct 19 '25 05:10

Dan H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!