Skip to content

Instantly share code, notes, and snippets.

@nsorosac
Created June 9, 2015 07:58
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 nsorosac/ab29a72682bd052e46bb to your computer and use it in GitHub Desktop.
Save nsorosac/ab29a72682bd052e46bb to your computer and use it in GitHub Desktop.
<?php
define('RECAPTCHA_PUBLIC_KEY', '');
define('RECAPTCHA_PRIVATE_KEY', '');
// Formulaire
<script src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha" data-sitekey="<?php echo RECAPTCHA_PUBLIC_KEY; ?>"></div>
// Validation du post
function checkRecaptcha()
{
if (isset($_POST['g-recaptcha-response'])) {
$recaptcha_response = urlencode($_POST['g-recaptcha-response']);
$private_key = urlencode(RECAPTCHA_PRIVATE_KEY);
$ip_address = urlencode(getRemoteIP());
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$private_key}&response={$recaptcha_response}&remoteip={$ip_address}");
$result = json_decode($response);
if (is_object($result) && isset($result->success))
return $result->success;
return false;
}
return true;
}
function getRemoteIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
$ip = $_SERVER['HTTP_CLIENT_IP'];
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
$ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment