I'm having troubles integrating reCAPTCHA v2 with my existing form. I would be grateful if someone has the solution to this.
This is my form page HTML code where I'm adding the widget:
before /head tag:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
In the body:
<div class='mod modContactForm'>
<form method="post" action="send.php" accept-charset="UTF-8"><p>
<label for='name'>Your Name</label>
<input class="input-text required" id="name" name="name" type="text" />
</p>
<p>
<label for='email'>Your Email</label>
<input class="input-text required email" id="email" name="email" type="text" />
</p>
<p>
<label for='message'>Your Message</label>
<textarea class="required" rows="5" cols="80" id="message" name="message"></textarea>
</p>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="--- MY SITE KEY HERE ---"></div>
<input class="button" type="submit" value="Send message">
<p class='thanks' style='display: none;'>
Thanks for contacting Video Production Edinburgh,<br />we'll be in touch within 24 hours!
</p>
</form>
Live page with not-working reCAPTCHA can be found here.
This is the send.php code on the server side to handle the form submission:
<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => '--- MY SECRET KEY HERE ----',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
} else {
// If CAPTCHA is successfully completed...
$to = "hello@videoproductionedinburgh.co.uk";
$name = $_POST['name'];
$subject = $_POST['subject'];
$body = stripslashes($_POST['message']);
$from = $_POST['email'];
$headers = "Reply-To: " . $from;
$error_messages = array();
if( strlen($name) < 1 ) {
$error_messages[] = "Name required";
}
if( strlen($subject) < 1 ) {
// set the default subject here
$subject = "Message from Video Production Edinburgh";
}
if( strlen($body) < 1 ) {
$error_messages[] = "Message required";
}
if( strlen($from) < 1 ) {
$error_messages[] = "Email required";
}
if(sizeof($error_messages) == 0 && mail($to, $subject, $body, $headers)) {
echo("success");
} else {
foreach( $error_messages as $error_message ) {
echo "$error_message <br />";
}
}
}
?>
At the moment the form submits successfully regardless of the reCAPTCHA being ticket or not.
Any help would be much apprecaited.
Thank you, Paolo