Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Last active November 29, 2016 13:08
Show Gist options
  • Save Anan5a/5eeb16e9e682ee63f69f9a31b0a1eb04 to your computer and use it in GitHub Desktop.
Save Anan5a/5eeb16e9e682ee63f69f9a31b0a1eb04 to your computer and use it in GitHub Desktop.
A function to verify Google reCAPTCHA response.
<?php
$response=$_POST['g-recaptcha-response'];
$secret='your_secret_key';
$json=g_rec_verify($response,$secret);
/*
function
*/
function g_rec_verify($res,$sec,$ip=null)
{
$post = "secret=$sec&response=$res".$ip!=null ?"&remoteip=$ip":"";
$ch = curl_init();
$op = array(
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded;
charset=utf-8', 'Content-Length: '.strlen($post)),
CURLOPT_POSTFIELDS => $post
);
curl_setopt_array($ch,$op);
$gresp = json_decode(curl_exec($ch));
curl_close($ch);
if($gresp->success)
{
return true;
}
else
{
return false;
}
}
//cheer
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment