簡體   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