reCaptcha on my form has a bug – PHP – SitePoint Forums

Hi people,

I postponed adding a captcha on my contact form until now the spam has become overwhelming.

It’s pretty much there, except for one bug that I hope you’ll help me with.

I use the following in my form’s .php file:

$captcha = isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response'] : null;
 
if(!is_null($captcha)){
	$res = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=MY-SECRET_KEY-HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']));
	if($res->success === true){

	}
	else{
		echo 'Forgot the captcha!!!';
	}
}

It goes to the very top of the file and everything else is in the nested if.

Everything seems to work well except when the captcha is correctly done but there are errors in entering the form. In this case, the user must go back and correct the errors. However, the captcha does not refresh and when the form is resubmitted, there is a captcha error.

Hope this makes sense and you can point out my errors.


How exactly does this happen? Your form and form processing code should be on the same page. If there are any validation errors, you should redisplay the form, generate a new recaptcha, and re-populate the user form fields with the existing values ​​so the user doesn’t have to re-enter data over and over again .



1 like

A message asks the user to go back.

Having the form and the form processing code on the same page might be a good solution, but that’s not what I did. I understand that generating a new recaptcha would be ideal. It doesn’t happen. That’s why I’m here.

It might help if I told you that the following is before the closing “head” tag:

And the recaptcha is inserted in the form just before the submit button:

James S. Joseph