Last active
April 25, 2017 08:40
-
-
Save EvilWolf/06aa90e0537ef7368d501e01509c2980 to your computer and use it in GitHub Desktop.
Подключение ractaptcha
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
Получаем код на сайте recaptcha | |
https://www.google.com/recaptcha/admin#list | |
//html | |
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?onload=reCaptchaLoadCallback&render=explicit" async defer></script> | |
//js | |
var reCaptchaLoadCallback = function() { | |
var sitekey = "6LeCRxIUAAAAAE10sOYS4gWW4zlRg8zp7XeSW7Za"; | |
$(function(){ | |
$('.g-recaptcha').each(function(i, e){ | |
grecaptcha.render(e, {'sitekey' : sitekey}); | |
}); | |
}); | |
} | |
//php | |
function verifyGoogleCaptcha($request) { | |
/* Посылает запрос верификации от GoogleCaptcha */ | |
$reCAPTCHASecretKey = '6LeCRxIUAAAAAHzrVDslKCO4NGrVa7V21MtRqt_0'; | |
$data = array( | |
'secret' => $reCAPTCHASecretKey, | |
'response' => $request, | |
'remoteip' => $_SERVER['REMOTE_ADDR'] | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify'); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'reCAPTCHA/PHP'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
$data = curl_exec($ch); | |
$data = json_decode($data, true); | |
if (!isset($data['success'])) /* Если что-то не то с $ch, или json */ | |
$data = array('success' => false); | |
return $data['success']; | |
} | |
// check in php | |
if (!verifyGoogleCaptcha($_POST['g-recaptcha-response'])) { | |
$err[] = 'Ошибка обработки reCaptcha'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment