Google html 表单的 ReCAPTCHA php

Google ReCAPTCHA for html form with php

最近我的网站上的垃圾邮件情况已经失控,我决定我真的必须对此做点什么。 google reCAPTCHA 似乎是区分人与机器人的最佳方式。

我在PHP方面没有太多经验,这让我很难完成实施。

HTML 我表格的一侧:

<div class="col-lg-6 no-gap contact-form">
            <header class="sec-heading">
                <br>
              <h2>Contact</h2>
              <span class="subheading">SUBHEADING</span>
            </header>

            <form action="assets/contact-form/contact-form.php" method="POST" class="form-ajax wow fadeInUp" data-wow-duration="1s" data-wow-delay=".1s">

              <!-- Name -->
              <div class="form-group">
                <input type="text" name="name" id="name-contact-1" class="form-control validate-locally" placeholder="Write your name">
                <label for="name-contact-1">Name</label>
                <span class="pull-right alert-error"></span>
              </div>

              <!-- Email -->
              <div class="form-group">
                <input type="email" name="email" id="email-contact-1" class="form-control validate-locally" placeholder="Your email">
                <label for="email-contact-1">Email</label>
                <span class="pull-right alert-error"></span>
              </div>

            <!-- Select -->
            <div class="form-group">
              <select class="form-control" id="select-form" name="select" id="select">

                <option id="optiona">Option A</option>
                <option id="optionb">Option B</option>
                <option id="optionc">Option C</option>
                <option id="optiond">Option D</option>
              </select>
              <label for="select-form">Your choice</label>
            </div>

             <!-- Select -->
            <div class="form-group">
              <select class="form-control" id="select-form" name="select1" id="select1">
                <option value="" disabled selected>Choose.. </option> 
                <option id="option1">Option 1</option>
                <option id="option2">Option 2</option>
                <option id="option3">Option 3</option>
                <option id="option4">Option 4</option>
              </select>
              <label for="select-form">Options</label>
            </div>


              <!-- Message -->
              <div class="form-group">
                <textarea class="form-control" name="message" id="message-contact-1" rows="5" placeholder="Your message"></textarea>
                <label for="message-contact-1">Message</label>
              </div>
             <div class="form-group">
              <div class="g-recaptcha" data-sitekey="---SECRET CODE FOR reCAPTCHA---"></div> 
              </div>
              <br>

              <input type="submit" name="submit" class="btn pull-right" value="Send message">

              <!-- Ajax Message -->
              <div class="ajax-message col-md-12 no-gap"></div>

            </form>

PHP 我表格的一侧:

 <?php 
    if(isset($_POST['email'])) {



    $email_to = "my@email.com";

    $email_subject = "Message recieved from form";





    function died($error) {


        echo '<div class="alert alert-danger alert-dismissible wow fadeInUp" role="alert">
          <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <strong>Oops! Something seems to be incorrect. Please check for errors:</strong><br>';

        echo $error."<br />";

        echo '</div>';

        die();

    }





        if(!isset($_POST['name']) ||

        !isset($_POST['email']) ||



        !isset($_POST['message'])) {

        died('Oops! Something seems to be incorrect. Please check for errors. ');       

    }



    $name = $_POST['name']; // required

    $email_from = $_POST['email']; // required

    $telephone = $_POST['phone']; // not required

    $message = $_POST['message']; // required

    $select = $_POST['select']; // required

    $select1 = $_POST['select1']; // required




    $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 .= 'The email adress you entered is invalid<br />';

  }

  if(strlen($name) < 2) {
  $error_message .= 'The name you have entered seems to be invalid<br />';

 }

    if(strlen($message) < 2) {

    $error_message .= 'The message you entered seems to be invalid<br />';

  }

    if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "A message was recieved: <br>\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }

    $email_message .= "<br><b>Name:</b> ".clean_string($name)."\n";

    $email_message .= "<br><b>Email:</b> ".clean_string($email_from)."\n";

    $email_message .= "<br><b>Options1:</b> ".clean_string($select)."\n";

    $email_message .= "<br><b>Options2:</b> ".clean_string($select1)."\n";

    $email_message .= "<br><b>Message:</b> ".clean_string($message)."\n";


$headers = 'From: '.$email_from."\r\n".
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  
 ?>

 <div class="alert alert-success alert-dismissible wow fadeInUp" role="alert">
   <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
   Thank you! Your message has been sent!
 </div>

 <?php } ?>

像我这样的新手,我一直在尝试按照有关如何完成这项工作的教程进行操作,因此我尝试在 php 文件的顶部实现一个函数,它看起来像:

    <?php

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => [
        'secret' => 'SECRET CODE FROM GOOGLE',
        'response' => $_POST['g-recaptcha-response'],

        ],
]);

$response = json_decode(curl_exec($curl));

if (!$response->success) {

但我似乎根本无法让它工作,而且我不知道如何计算应该生成错误消息的“}else{”部分。

有经验的人能帮帮我吗?

谢谢,

最肮脏的方法是用 che recaptcha-check 代码包装您的实际 email-sending 代码。首先,将 died() 函数移到 if {..} 语句之外。您可以将该函数重用为一个非常简单的错误处理程序。

<?php
//recaptcha test
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_POSTFIELDS => [
        'secret' => 'SECRET CODE FROM GOOGLE',
        'response' => $_POST['g-recaptcha-response'],
        ],
]);
$response = json_decode(curl_exec($curl));

//UPDATE: if your check pass, go on
if ($response->success) {
    //your actual code.
} else {
    died("Recaptcha missing.");
}
?>