-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-captcha.php
43 lines (31 loc) · 1.38 KB
/
google-captcha.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Helper
$doc->addScript("https://www.google.com/recaptcha/api.js?render=Ключ сайта");
// Script
function resetCapcha() {
grecaptcha.execute('Ключ сайта', { action: 'contact' }).then(function (token) {
document.querySelectorAll(".recaptchaResponseMulti").forEach(elem => (elem.value = token));
});
}
grecaptcha.ready(function () {
grecaptcha.execute('6LeQDyQaAAAAAKhuMRRP0PEAYq7-sIrYLAqfam-9', { action: 'contact' }).then(function (token) {
document.querySelectorAll(".recaptchaResponseMulti").forEach(elem => (elem.value = token));
});
});
// HTML
<input type="hidden" name="recaptcha_response" id="recaptchaResponse" class="recaptchaResponseMulti" value="">
// AJAX
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recaptcha_response'])) {
// Build POST request
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'Секретный ключ';
$recaptcha_response = $_POST['recaptcha_response'];
// Make and decode POST request
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
// Take action based on the score returned
if ($recaptcha->score >= 0.5) {
// Verified - send email
} else {
exit;
}
}