Last active
July 14, 2020 20:10
-
-
Save diversen/8184ecfe8b796dc809c3cb2c514003a0 to your computer and use it in GitHub Desktop.
php verify google recaptcha version 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This line:
$response = json_decode(file_get_contents_curl(buildCaptchaUrl()), true);
Should be:
$response = json_decode(fileGetContentsCurl(buildCaptchaUrl()), true);
Thanks!