Created
June 9, 2015 07:58
-
-
Save nsorosac/ab29a72682bd052e46bb to your computer and use it in GitHub Desktop.
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 | |
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