PHP - Validate the recaptcha v2 response
12,546
Remove this line:
$captcha = $_GET["g-recaptcha-response"];
Then you need to decode the json by Google like so:
$g_response = json_decode($response);
Then just check with if/else:
if ($g_response->success === true) echo "success!";
Author by
baconck
Updated on June 24, 2022Comments
-
baconck almost 2 years
#form.php if(isset($_POST['g-recaptcha-response'])){ $captcha=$_POST['g-recaptcha-response']; $captcha=$_GET["g-recaptcha-response"]; $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=__1234__&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']); echo $response;
Returns:
Notice: Undefined index: g-recaptcha-response in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 114 { "success": false, "error-codes": [ "missing-input-response" ] }
echo $response->success;
Returns:
Notice: Trying to get property of non-object in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 119
I just need to get the "success" object, then I can test if it is True OR False and I am done.
-
joyBlanks over 8 yearstry
var_dump($_POST)
check if the key value you are seeking exists -
baconck over 8 yearsecho $response; returns { "success": false, "error-codes": [ "missing-input-response" ] } so the string is empty making the response false. I need to get the "success" value.
-
Admin over 8 yearsthis is not the defult provided by google php libairy, have you tried that
-
baconck over 8 yearsrecaptcha v2 does not provide a php lib that I know about...???
-
devman over 5 yearsyou shouldn't post your secret on the internet btw
-
vandijkstef about 5 yearsPlease note that you've included your secret key in the question. Please don't do that again
-