/
recaptchalib.php
45 lines (34 loc) · 967 Bytes
/
recaptchalib.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
44
45
<?php
// new php7 captcha v2 implementation.
function recaptcha_check_answer($key, $response){
return CheckCaptcha($key, $response);
}
function CheckCaptcha($key, $response) {
try {
$url = 'https://recaptcha.net/recaptcha/api/siteverify';
$dat = array(
'secret' => $key,
'response' => urlencode($response),
'remoteip' => urlencode($_SERVER['REMOTE_ADDR'])
);
$opt = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($dat)
)
);
$context = stream_context_create($opt);
$result = file_get_contents($url, false, $context);
return json_decode($result)->success;
} catch (Exception $e) {
return null;
}
}
function recaptcha_get_html($pubKey){
return "
<script src='https://recaptcha.net/recaptcha/api.js'></script>
<br /> <div class='g-recaptcha' data-theme='dark' data-sitekey='" . $pubKey . "'></div>
";
}
?>