Home
PHP
Tech Tube
MySQL
Linux
CSS&HTML
JavaScript

Recaptcha validation

The method above validates reCaptcha input.
/**
* Validate reCaptcha.
* @author Samuil Banti
* @param string $recapcha - The value of the recaptcha input ('g-recaptcha-response').
* @param string $secret - The secret key of your reCaptcha.
* @return boolean $response->success - The status of the reCaptcha validation.
* @license GNU/GPLv3<a href=" http://www.gnu.org/licenses/gpl-3.0.html" target="_blank"> http://www.gnu.org/licenses/gpl-3.0.html</a> 
*/
function recapcha($recapcha, $secret) {
	$start = microtime(true);
	$url = 'https://www.google.com/recaptcha/api/siteverify';

	$data = array(
		'secret' => $secret,
		'response' => $recapcha,
	);
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	$response = curl_exec($curl);
	curl_close($curl);
	$response = json_decode($response);
	$time_elapsed_secs = microtime(true) - $start;
	return $response->success;
}
But since the reCaptcha validation response usually takes around a second which is a lot of time this days here's a regular captcha alternative: https://samiwell.eu/php/php-captcha-class