OdinsHat
9/10/2017 - 3:48 PM

Basic Recaptcha Implementation in PHP

Basic Recaptcha Implementation in PHP

/**
 * 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 
<!-- 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>