Skip to content

Instantly share code, notes, and snippets.

@emmgfx
Created July 20, 2016 08:07
Show Gist options
  • Save emmgfx/213410e887f594e466dd996a571f033d to your computer and use it in GitHub Desktop.
Save emmgfx/213410e887f594e466dd996a571f033d to your computer and use it in GitHub Desktop.
Check Recaptcha
<?php
if(checkRecaptcha($_POST['g-recaptcha-response'])){
# Recaptcha OK
}else{
# Recaptcha KO
}
function checkRecaptcha($recaptcha_response){
$url = 'https://www.google.com/recaptcha/api/siteverify';
$fields = array(
'secret' => 'SECRETKEY',
'response' => $recaptcha_response,
);
$fields_string = '';
foreach($fields as $key => $value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$recaptcha_query = json_decode(curl_exec($ch), true);
curl_close($ch);
return ($recaptcha_query['success'] == 1);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment