Created
July 20, 2016 08:07
-
-
Save emmgfx/213410e887f594e466dd996a571f033d to your computer and use it in GitHub Desktop.
Check Recaptcha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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