Skip to content

Instantly share code, notes, and snippets.

@diversen

diversen/php-recaptcha.php

Last active Jul 14, 2020
Embed
What would you like to do?
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;
}
@komputronika

This comment has been minimized.

Copy link

@komputronika komputronika commented Jun 7, 2018

This line:
$response = json_decode(file_get_contents_curl(buildCaptchaUrl()), true);

Should be:
$response = json_decode(fileGetContentsCurl(buildCaptchaUrl()), true);

Thanks!

@diversen

This comment has been minimized.

Copy link
Owner Author

@diversen diversen commented Sep 11, 2018

@komputronika

Thanks 👍

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