Skip to content

Instantly share code, notes, and snippets.

@EvilWolf
Last active April 25, 2017 08:40
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 EvilWolf/06aa90e0537ef7368d501e01509c2980 to your computer and use it in GitHub Desktop.
Save EvilWolf/06aa90e0537ef7368d501e01509c2980 to your computer and use it in GitHub Desktop.
Подключение ractaptcha
Получаем код на сайте 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