繁体   English   中英

将Google Re-CAPTCHA表单添加到PHP“联系我们表单”

[英]Adding a Google Re-CAPTCHA form to PHP “Contact us form”

我是网络开发方面的新手,我遇到了一个联系表单的问题,我试图附加一个验证码字段以防止发送垃圾邮件。

在添加验证码表单之前,代码工作并发送电子邮件。添加验证码验证后,JS文件显示成功确认消息,但未发送电子邮件。

代码我们详述如下:

HTML:

 <form name="sentMessage" id="contactForm" novalidate>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                    //rest of the inputs 
                    <div class="form-group">
                            <div class="g-recaptcha" data-sitekey="site_key_here"></div>
                            <span class="help-block" style="display: none;">Please check that you are not a robot.</span>
                    </div>
                    <br>
                    <div id="success"></div>
                    <div class="row">
                        <div class="form-group col-xs-12">
                            <button type="submit" class="btn btn-success btn-lg" id="submit">Send</button>
                        </div>
                    </div>
                </form>

PHP:

<?php

function errorResponse ($messsage) {
  header('HTTP/1.1 500 Internal Server Error');
  return false;
}

// Check for empty fields
if(empty($_POST['name']) ||  empty($_POST['email']) ||     empty($_POST['phone'])   || empty($_POST['message']) ||     !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
  echo "No arguments Provided!";
  return false;
}

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
$to = '_email_here_';
$email_subject = "Contact Form from website:  $name";
$email_body = "You have received a new message from your website contact   form.\n\n"."Here are the details:\n\nName: $name\n\nEmail:   $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address"; 

//MY CODE STARTS HERE
// getting the captcha
$captcha = "";
if (isset($_POST["g-recaptcha-response"])) {
      $captcha = $_POST["g-recaptcha-response"];
}
else {
    return false;
}

// handling the captcha and checking if it's ok
$secret = "_secret_key_here_";
$google_url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"];
$google_response = file_get_contents($google_url);

$response = json_decode($google_response, true);
// if the captcha is cleared with google, send the mail and return.
if ($response["success"] != false) {
    // send the actual mail
    mail($to,$email_subject,$email_body,$headers);
    // return goes back to the ajax, so the user can know if everything is ok
    return true;
} else {
    return false;
}
//AND ENDS HERE
?>

在验证码验证之前,代码是:

mail($to,$email_subject,$email_body,$headers);
return true;

解决方案在这里找到: https//bootstrapious.com/p/bootstrap-recaptcha主题已关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM