public function processForm() { if ($this->method == "POST") { $this->checkRequiredFields(); $recaptcha = new GoogleRecaptcha(); if ($recaptcha->verify($_POST['g-recaptcha-response'])) { return $this->sendEmail($_POST['email'], $_POST['name'], $_POST['message']); } else { throw new \Exception("Could not verify captcha, try again."); } } return false; }
{ $url = $this->google_url . "?secret=" . $this->secret . "&response=" . $response; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_TIMEOUT, 15); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); $curlData = curl_exec($curl); curl_close($curl); $res = json_decode($curlData, TRUE); if ($res['success'] == 'true') { return TRUE; } else { return FALSE; } } } $message = 'Google reCaptcha'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $response = $_POST['g-recaptcha-response']; if (!empty($response)) { $cap = new GoogleRecaptcha(); $verified = $cap->VerifyCaptcha($response); if ($verified) { $message = "Captcha Success!"; } else { $message = "Please reenter captcha"; } } }
$curlData = curl_exec($curl); curl_close($curl); $response = json_decode($curlData, true); $recaptchaResponse = new Response(); $recaptchaResponse->isValid = $response["success"]; $recaptchaResponse->errors = $response["error-codes"]; return $recaptchaResponse; } } if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message']) && isset($_POST['g-recaptcha-response'])) { if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) { $name = strip_tags($_POST['name']); $email = strip_tags($_POST['email']); $message = strip_tags($_POST['message']); // reCAPTCHA $gRecaptcha = new GoogleRecaptcha(); $response = $gRecaptcha->verifyRecaptcha($_POST['g-recaptcha-response']); if (!$response->isValid) { // What happens when the CAPTCHA was entered incorrectly $response->message = "Bad reCaptcha verification!"; die($response->toJson()); } else { // Your code here to handle a successful verification // Generate email and send! $to = $myemail; $email_subject = "PISTA User Contact Form: {$name}"; $email_body = "Name: {$name} \n " . "Email: {$email}\n" . "Message: {$message}\n"; $headers = "From: {$myemail}\n"; $headers .= "Reply-To: {$email}"; mail($to, $email_subject, $email_body, $headers); $response->message = "Mail was sent.";