Skip to content

Instantly share code, notes, and snippets.

@OdinsHat
Created September 10, 2017 15:48
Show Gist options
  • Save OdinsHat/acfa7a39255127bb358b6bf5de5c9e0a to your computer and use it in GitHub Desktop.
Save OdinsHat/acfa7a39255127bb358b6bf5de5c9e0a to your computer and use it in GitHub Desktop.
Basic Recaptcha Implementation in PHP
<!-- Add htis to the <head> of the page -->
<script src='https://www.google.com/recaptcha/api.js'></script>
<!-- Add this to the location of wher eyou want the recaptcha to go -->
<div class="g-recaptcha" data-sitekey="SITEKKEY"></div>
/**
* More inormation on Google's Recapture service is avilable here: https://developers.google.com/recaptcha/
*/
$ch = curl_init();
$postfields = array(
'secret' => 'YOURSECRET',
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR'] //OPTIONAL
);
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$server_output = curl_exec ($ch);
curl_close($ch);
$result = json_decode($server_output);
// Check $result['success'] == true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment