正确的PHP for reCaptcha

时间:2018-11-17 16:44:55

标签: php recaptcha

我有一个包含php的电子邮件表单,但想向其中添加reCaptcha,但是我不知道在哪里将新的代码部分添加到已有的php中。我已经在“ die();}”之后添加了它。这种工作方式是:提交后弹出“成功html页面”时,它将我的原始html消息与“ You are spammer!从新添加的php中获取@ $%K。

我的电子邮件表单中包含以下php。

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

          // EDIT THE 2 LINES BELOW AS REQUIRED
          $email_to = "info@acupuntura-vilanova.es";
          $email_subject = "email acupuntura esp";


          function died($error) {
            // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
          }

          // validation expected data exists
          if(!isset($_POST['first_name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['telephone']) ||
            !isset($_POST['comments'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');   
          }

          $first_name = $_POST['first_name']; // required
          $email_from = $_POST['email']; // required
          $telephone = $_POST['telephone']; // not required
          $comments = $_POST['comments']; // 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 Address you entered does not appear to be valid.<br />';
         }
          $string_exp = "/^[A-Za-z .'-]+$/";
         if(!preg_match($string_exp,$first_name)) {
          $error_message .= 'The First Name you entered does not appear to be valid.<br />';
         }
         if(strlen($comments) < 2) {
          $error_message .= 'The Comments you entered do not appear to be valid.<br />';
         }
         if(strlen($error_message) > 0) {
          died($error_message);
         }
          $email_message = "Form details below.\n\n";

          function clean_string($string) {
           $bad = array("content-type","bcc:","to:","cc:","href");
           return str_replace($bad,"",$string);
          }

          $email_message .= "First Name: ".clean_string($first_name)."\n";
          $email_message .= "Email: ".clean_string($email_from)."\n";
          $email_message .= "Telephone: ".clean_string($telephone)."\n";
          $email_message .= "Comments: ".clean_string($comments)."\n";


        // create email headers
        $headers = 'From: '.$email_from."\r\n".
        'Reply-To: '.$email_from."\r\n" .
        'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers); 
        ?>

        <!-- include your own success html here -->
...

        <?php
        }
        ?>

我需要在以下部分添加一个reCaptcha。

$email;$comment;$captcha;
        if(isset($_POST['email'])){
          $email=$_POST['email'];
        }if(isset($_POST['comment'])){
          $email=$_POST['comment'];
        }if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        $secretKey = "Put your secret key here";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) {
          echo '<h2>You are spammer ! Get the @$%K out</h2>';
        } else {
          echo '<h2>Thanks for posting comment.</h2>';
        }

如何合并这两段代码?

0 个答案:

没有答案