Hello everybody
After some time searching for a solution I found your site and one of your articles which is very similair to my problem.
My Task: I have a contact form on my homepage to which I would like to add a Google reCAPTCHA.
Problem: The contact form works without a CAPTCHA, but as soon as I try to implement one, it doesn’t work at all.
The contact form is the following:
<form id="contact-form" method="POST" action="send_email.php" role="form">
<div class="form-group">
<input type="text" placeholder="Your Name" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<input type="email" placeholder="Your Mail" class="form-control" name="email" id="email" >
</div>
<div class="form-group">
<input type="text" placeholder="Subject" class="form-control" name="subject" id="subject">
</div>
<div class="form-group">
<textarea rows="6" placeholder="Your message" class="form-control" name="message" id="message"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="<--PUBLIC SITE KEY-->"></div>
<div id="submit">
<input type="submit" id="contact-submit" class="btn btn-default btn-send send-button" name="contact-submit" value="Send message">
</div>
</form>
After the submit button is pressed, it will go to send_email.php. This File is a combination out of the part I added for the CAPTCHA:
<?php
echo "1";
$yoursecret = "<--SECRET SITE KEY-->";
echo "2";
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $yoursecret."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$googleobj = json_decode($response);
$verified = $googleobj->success;
echo "3";
if ($verified === true){
echo "4";
The normal part which works on itself:
if(isset($_POST['email'])) {
$email_to = "my@mail.com";
$email_subject = "Contact Form";
echo "5";
function died($error) {
// error
echo "Following mistake appeared:<br /><br />";
echo $error."<br /><br />";
echo "Please check your submission.<br /><br />";
die();
}
echo "6";
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['comments'])) {
died('A Problem appeared.');
}
echo "7";
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // not required
$comments = $_POST['comments']; // required
echo "8";
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'Your mail is not valid.<br />';
}
echo "9";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'Your name is not valid.<br />';
}
echo "10";
if(strlen($comments) < 2) {
$error_message .= 'Your message is empty or not valid.<br />';
}
echo "11";
if(strlen($error_message) > 0) {
died($error_message);
}
echo "12";
$email_message = "Contact Form \n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
echo "13";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
echo "14";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
echo "15";
and the end of the part added for the CAPTCHA:
died('SUCCESSFUL');
}
else{
echo "16";
died('ERROR');
}
}
I also added:
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
Thank you for all your help!