Skip to content

Instantly share code, notes, and snippets.

@thblckjkr
Created February 24, 2019 21:50
Show Gist options
  • Save thblckjkr/d1dc8085577a7ba3980e922c6b230922 to your computer and use it in GitHub Desktop.
Save thblckjkr/d1dc8085577a7ba3980e922c6b230922 to your computer and use it in GitHub Desktop.
Verify google reCAPTCHA with PHP. Clean and universal function.
<?php
/*
Decided to put this function as a gist itself, taken from this great comment
https://gist.github.com/jonathanstark/dfb30bdfb522318fc819#gistcomment-2733991
*/
function validate_rechapcha($response){
// Verifying the user's response (https://developers.google.com/recaptcha/docs/verify)
$verifyURL = 'https://www.google.com/recaptcha/api/siteverify';
// Collect and build POST data
$post_data = http_build_query(
array(
'secret' => 'YOUR_SITE_KEY',
'response' => $response,
'remoteip' => (isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER['REMOTE_ADDR'])
)
);
// Send data on the best possible way
if(function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec')) {
// Use cURL to get data 10x faster than using file_get_contents or other methods
$ch = curl_init($verifyURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-type: application/x-www-form-urlencoded'));
$response = curl_exec($ch);
curl_close($ch);
} else {
// If server not have active cURL module, use file_get_contents
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($opts);
$response = file_get_contents($verifyURL, false, $context);
}
// Verify all reponses and avoid PHP errors
if($response) {
$result = json_decode($response);
if ($result->success===true) {
return true;
} else {
return $result;
}
}
// Dead end
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment