2016-02-19 28 views
0

試圖實施與谷歌reCaptcha自定義編碼的聯繫表格,但似乎沒有采取。這是使用WordPress的(不希望使用插件'驗證回來了「謝謝張貼評論」。不要從表格中的電子郵件/消息,檢查我的垃圾郵件等。任何幫助將不勝感激。如下:與谷歌reCaptcha自定義聯繫表格

<div class="contact_form"> 
     <h1>Contact Form</h1> 
     <form id="comment_form" action="" method="post"> 
      <p>Your Name (required) <br /> 
      <input type="text" name="cf-name" placeholder="type your name" pattern="[a-zA-Z0-9 ]+" size="40" /> 
      </p> 
      <p>Your Email (required) <br /> 
      <input type ="email" name="cf-email" placeholder="type your email" size ="40"><br> 
      </p> 
      <p>Subject (required)<br /> 
      <input type ="text" name="cf-subject" placeholder="type your subject" size ="40"><br> 
      </p> 
      <p>Your Message<br /> 
      <textarea rows="10" cols="35" name="cf-message"></textarea> 
      </p> 
      <div class="g-recaptcha" data-sitekey="SITEKEYHERE"></div> 
      <input type="submit" name="submit" value="Post Comment"> 
     </form> 
     </div> 

而且我的PHP來發送電子郵件,並檢查驗證碼:

<?php 

    // if the submit button is clicked, send the email 
    if (isset($_POST['cf-submitted'])) { 

     // sanitize form values 
     $name = sanitize_text_field($_POST["cf-name"]); 
     $email = sanitize_email($_POST["cf-email"]); 
     $subject = sanitize_text_field($_POST["cf-subject"]); 
     $message = esc_textarea($_POST["cf-message"]); 

     // get the blog administrator's email address 
     $to = "[email protected]"; 

     $headers = "From: $name <$email>" . "\r\n"; 

     // If email has been process for sending, display a success message 
     if (wp_mail($to, $subject, $message, $headers)) { 
      echo '<div>'; 
      echo '<p>Thanks for contacting me, expect a response soon.</p>'; 
      echo "$to"; 
      echo '</div>'; 
     } else { 
      echo 'An unexpected error occurred'; 
      } 
    }?> 


    <?php 

     $name;$email;$subject;$message;$captcha; 
     if(isset($_POST['cf-name'])){ 
      $email=$_POST['cf-name']; 
     }if(isset($_POST['cf-email'])){ 
      $email=$_POST['cf-email']; 
     }if(isset($_POST['cf-subject'])){ 
      $email=$_POST['cf-subject']; 
     }if(isset($_POST['cf-message'])){ 
      $email=$_POST['cf-message']; 
     }if(isset($_POST['g-recaptcha-response'])){ 
      $captcha=$_POST['g-recaptcha-response']; 
     } 
     if(!$captcha){ 
      echo '<h2>Please check the the captcha form.</h2>'; 
      exit; 
     } 
     $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEYHERE".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']); 
     if($response.success==false) 
     { 
      echo '<h2>spam</h2>'; 
     }else 
     { 
      echo '<h2>Thanks for posting comment.</h2>'; 
     } 
?> 

回答

0

你的郵件代碼是在你驗證您的驗證碼之前觸發

試着走動代碼:

<?php 

    $name;$email;$subject;$message;$captcha; 
    if(isset($_POST['cf-name'])){ 
     $email=$_POST['cf-name']; 
    }if(isset($_POST['cf-email'])){ 
     $email=$_POST['cf-email']; 
    }if(isset($_POST['cf-subject'])){ 
     $email=$_POST['cf-subject']; 
    }if(isset($_POST['cf-message'])){ 
     $email=$_POST['cf-message']; 
    }if(isset($_POST['g-recaptcha-response'])){ 
     $captcha=$_POST['g-recaptcha-response']; 
    } 
    if(!$captcha){ 
     echo '<h2>Please check the the captcha form.</h2>'; 
     exit; 
    } 
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEYHERE".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']); 
    if($response.success==false){ 
     echo '<h2>spam</h2>'; 
    }else{ 
     // sanitize form values 
     $name = sanitize_text_field($_POST["cf-name"]); 
     $email = sanitize_email($_POST["cf-email"]); 
     $subject = sanitize_text_field($_POST["cf-subject"]); 
     $message = esc_textarea($_POST["cf-message"]); 

     // get the blog administrator's email address 
     $to = "[email protected]"; 

     $headers = "From: $name <$email>" . "\r\n"; 

     // If email has been process for sending, display a success message 
     if (wp_mail($to, $subject, $message, $headers)) { 
      echo '<div>'; 
      echo '<p>Thanks for contacting me, expect a response soon.</p>'; 
      echo "$to"; 
      echo '</div>'; 
     } else { 
      echo 'An unexpected error occurred'; 
     }   

    } 
?> 

我在手機上點擊了這個,所以你可以整理這個代碼了很多。 但邏輯應該是對的。