Skip to content

Instantly share code, notes, and snippets.

@luckydevilru
Last active June 25, 2019 06:29
Show Gist options
  • Select an option

  • Save luckydevilru/684d64c31094ae4d12ee2979c688c0df to your computer and use it in GitHub Desktop.

Select an option

Save luckydevilru/684d64c31094ae4d12ee2979c688c0df to your computer and use it in GitHub Desktop.
Google recaptcha v3
<!doctype html>
<html lang="ru">
<head>
<script src='https://www.google.com/recaptcha/api.js?render=_____KEY______'></script>
</head>
<body>
<script>
$('.i-recaptcha').submit(function(e) {
e.preventDefault();
var ins = $(this).serialize();
grecaptcha.execute('_____KEY______', {action: 'action_name'}).then(function(token) {
var ins2 = ins + "&DATA[TOKEN]="+token; // для отправки токена получаемого от гугла в обработчик, оттуда проверка гуглом.
console.log( ins2 );
// $.post( "/submit.php", ins, function(response){ console.log(response); } );
$.ajax({
type: "POST",
url: "/submit.php",
data: ins2,
success: function(response){
console.log(response);
},
error: function (jqXHR, textStatus, errorThrown){ console.log(jqXHR+textStatus+errorThrown); }
});
});
});
</script>
</body>
</html>
<?php
// ini_set('error_reporting', E_ALL);
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
$ip = $_SERVER['HTTP_X_REAL_IP'];
$leadData2 = $_POST['DATA'];
$note=''; //возвращаемые данные.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$url_google_api = 'https://www.google.com/recaptcha/api/siteverify';
$secret = '______SECRET-KEY______';
// Recaptcha V3
$query = $url_google_api . '?secret=' . $secret . '&response=' . $leadData2['TOKEN'] . '&remoteip=' . $ip;
$data = json_decode(file_get_contents($query));
if ($data->success) {
if (preg_match('/^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$/', $leadData2['PHONE_MOBILE'])==0) {
$note='Пожалуйста проверьте вводимые данные.';
}else{
$note='Спасибо! Ваша заявка принята! В ближайшее время с вами свяжется оператор. Пожалуйста, включите Ваш контактный телефон.';
}
}else{
$note='reCAPTCHA error: Check to make sure your keys match the registered domain and are in the correct locations. You may also want to doublecheck your code for typos or syntax errors.<br><br><a href="javascript:%20history.go(-1)">Вернуться на предыдущую страницу</a>';
}
}else{
$note='Внесистемная передача данных! Просим вас угомонить свой пердак';
}
echo $note;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment