Skip to content

Instantly share code, notes, and snippets.

@diversen
Last active July 14, 2020 20:10
Show Gist options
  • Save diversen/8184ecfe8b796dc809c3cb2c514003a0 to your computer and use it in GitHub Desktop.
Save diversen/8184ecfe8b796dc809c3cb2c514003a0 to your computer and use it in GitHub Desktop.
php verify google recaptcha version 2
<?php
/**
* Will make it work if server does not have
* allow_url_fopen
*/
function fileGetContentsCurl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* Build the captcha request URL
*/
function buildCaptchaUrl()
{
$captcha = $_POST['g-recaptcha-response'];
$secret = 'secret';
return "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR'];
}
/**
* Sends the captcha and returns true on success - else false
*/
function sendCaptchaResponse()
{
$response = json_decode(fileGetContentsCurl(buildCaptchaUrl()), true);
if ($response['success'] == false) {
return false;
}
return true;
}
@diversen
Copy link
Author

@komputronika

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment