What are common reasons for the "invalid captcha" error when using reCaptcha in PHP forms?
The "invalid captcha" error in reCaptcha for PHP forms commonly occurs when the user fails to correctly complete the captcha challenge. To solve this issue, you can validate the user's reCaptcha response on the server-side before processing the form submission. This involves verifying the user's response with Google's reCaptcha API.
// Validate reCaptcha response
$secret_key = 'YOUR_SECRET_KEY';
$captcha_response = $_POST['g-recaptcha-response'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret_key&response=$captcha_response");
$response_keys = json_decode($response, true);
if(intval($response_keys["success"]) !== 1) {
// Captcha verification failed
// Handle the error accordingly
} else {
// Captcha verification successful
// Process the form submission
}