· 6 years ago · Jun 13, 2019, 12:02 PM
1<?php
2 $email;$comment;$captcha;
3 $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
4 $comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING);
5 $captcha = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING);
6 if(!$captcha){
7 echo '<h2>Please check the the captcha form.</h2>';
8 exit;
9 }
10 $secretKey = "-----put your secret here------";
11 $ip = $_SERVER['REMOTE_ADDR'];
12
13 // post request to server
14 $url = 'https://www.google.com/recaptcha/api/siteverify';
15 $data = array('secret' => $secretKey, 'response' => $captcha);
16
17 $options = array(
18 'http' => array(
19 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
20 'method' => 'POST',
21 'content' => http_build_query($data)
22 )
23 );
24 $context = stream_context_create($options);
25 $response = file_get_contents($url, false, $context);
26 $responseKeys = json_decode($response,true);
27 header('Content-type: application/json');
28 if($responseKeys["success"]) {
29 echo json_encode(array('success' => 'true'));
30 } else {
31 echo json_encode(array('success' => 'false'));
32 }
33?>