Skip to content

Instantly share code, notes, and snippets.

@chiedo
Last active April 5, 2018 08:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chiedo/1c6be3b53ef675a94855 to your computer and use it in GitHub Desktop.
Save chiedo/1c6be3b53ef675a94855 to your computer and use it in GitHub Desktop.
PHP Verification Function for Google recaptcha Server side code
<?php
/*
* Verifies if the captcha data via Google and redirects to the form page if the captcha does not pass.
*
* Parameters:
* Gcaptcha response - the form data submitted with the request.
** Example call if your form used method='get': verify_captcha($_GET['g-recaptcha-response'], "Secret key here");
** Example call if your form used method='post': verify_captcha($_POST['g-recaptcha-response'], "Secret key here");
*/
function verify_google_captcha($gcaptcha_response, $secret) {
//open a new curl connection
$ch = curl_init();
$url = "https://www.google.com/recaptcha/api/siteverify";
$fields = array(
"secret" => $secret,
"response" => $gcaptcha_response,
);
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post request
$result = curl_exec($ch);
//close connection
curl_close($ch);
$json_data = json_decode($result);
//If the captcha was declined, redirect the user to the same page with the query var "captcha" set to "not-entered"
if($json_data->success == false) {
$redirect_to = $_SERVER["HTTP_REFERER"];
if(!strpos($redirect_to, "?")):
$redirect_to.="?captcha=not-entered";
else:
$redirect_to.="&captcha=not-entered";
endif;
header("Location: " . $redirect_to);
exit;
}
}
?>
@nisdis
Copy link

nisdis commented May 8, 2017

This works well, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment