kodie of Happy Medium
8/3/2016 - 2:58 PM

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 hi

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.';
}
?>