Skip to content

Instantly share code, notes, and snippets.

@kodie
Last active June 6, 2016 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kodie/26ec209885e19161ba67 to your computer and use it in GitHub Desktop.
Save kodie/26ec209885e19161ba67 to your computer and use it in GitHub Desktop.
Use this if you want your form to be submitted to a third party but also want to verify reCAPTCHA before it gets sent. You need to make a hidden input called 'sendURL' and in the value put the URL that you would like the form to be submitted to.
<?php
ob_start();
session_start();
require_once('recaptchalib.php');
$key_public = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$key_private = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$captchaFailed = false;
$response = null;
$reCaptcha = new ReCaptcha($key_private);
if (!function_exists('curl_version')) {
die('Curl package missing.');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['g-recaptcha-response'])) {
$response = $reCaptcha->verifyResponse(
$_SERVER['REMOTE_ADDR'],
$_POST['g-recaptcha-response']
);
if ($response !== null) {
if ($response->success) {
$fields = $_POST;
$sendURL = $fields['sendURL'];
unset($fields['sendURL']);
unset($fields['g-recaptcha-response']);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sendURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
} else {
$captchaFailed = true;
}
} else {
$captchaFailed = true;
}
} else {
$captchaFailed = true;
}
}
if ($captchaFailed && count(get_included_files()) < 3) {
echo 'reCAPTCHA failed! Please go back and try again.';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment