2014-01-21 45 views
0

我試圖根據this tutorial如何讓「簡單的驗證碼」以簡單的聯繫表單工作?

爲了避免垃圾郵件攻擊創建一個簡單的接觸形式,我想的接觸形式與really simple captcha plugin.

結合了下面的代碼,我能夠使用簡碼[contact]任何我的網頁和表格顯示,但檢查驗證碼時出現問題。

這是插件的代碼:

function wptuts_get_the_ip() { 
    if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { 
     return $_SERVER["HTTP_X_FORWARDED_FOR"]; 
    } 
    elseif (isset($_SERVER["HTTP_CLIENT_IP"])) { 
     return $_SERVER["HTTP_CLIENT_IP"]; 
    } 
    else { 
     return $_SERVER["REMOTE_ADDR"]; 
    } 
} 
// short code function 
function wptuts_contact_form_sc($atts) {  
add_shortcode('contact', 'contact_form_shortcode'); 

    extract(shortcode_atts(array(
     "email" => get_bloginfo('admin_email'), 
     "subject" => '', 
     "label_name" => 'Your Name', 
     "label_email" => 'Your E-mail Address', 
     "label_subject" => 'Your Answer', 
     "label_captcha" => 'Captcha', 
     "label_submit" => 'Submit', 
     "error_empty" => 'Please fill in all the required fields.', 
     "error_noemail" => 'Please enter a valid e-mail address.', 
     "success" => 'Thanks for your e-mail! We\'ll get back to you as soon as we can.' 
    ), $atts)); 

    $captcha_instance = new ReallySimpleCaptcha(); //imports really simple captcha plugin 
    $word = $captcha_instance->generate_random_word(); 
    $prefix = mt_rand(); 
    $image= $captcha_instance->generate_image($prefix, $word); 

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

     $error = false; 
     $required_fields = array("your_name", "email", "captcha", "subject"); 

     foreach ($_POST as $field => $value) { 
      if (get_magic_quotes_gpc()) { 
       $value = stripslashes($value); 
      } 
      $form_data[$field] = strip_tags($value); 
     } 

     foreach ($required_fields as $required_field) { 
      $value = trim($form_data[$required_field]); 
      if(empty($value)) { 
       $error = true; 
       $result = $error_empty; 
      } 
     } 

     if(!is_email($form_data['email'])) { 
      $error = true; 
      $result = $error_noemail; 
     } 

     if ($error == false) { 

      $captcha_text=$form_data['captcha'];     
      $correct = $captcha_instance->check($prefix, $captcha_text); 

      if($correct==TRUE){ 

       $email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject']; 
       $email_message = $form_data['captcha'] . "\n\nIP: " . wptuts_get_the_ip(); 
       $headers = "From: ".$form_data['your_name']." <".$form_data['email'].">\n"; 
       $headers .= "Content-Type: text/plain; charset=UTF-8\n"; 
       $headers .= "Content-Transfer-Encoding: 8bit\n"; 
       wp_mail($email, $email_subject, $email_message, $headers); 
       $result = $success; 
       $sent = true; 
      } 
     } 
    } 

    if($result != "") { 
     $info = '<div class="info">'.$result.'</div>';   
    } 

    $email_form = '<form class="contact-form" method="post" action="'.get_permalink().'"> 
     <div> 
      <label for="cf_name">'.$label_name.':</label> 
      <input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" /> 
     </div> 
     <div> 
      <label for="cf_email">'.$label_email.':</label> 
      <input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" /> 
     </div> 
     <div> 
      <label for="cf_subject">'.$label_subject.':</label> 
      <input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" /> 
     </div> 
     <div> 
      <label for="cf_message">'.$label_captcha.':</label> 
      <input type="text" name="captcha" id="cf_message" size="50" maxlength="50" value=""/> 
     </div> 
     <div id="captcha"><span id="captcha_text">Captcha:</span><img id="captcha_image" src="'.plugins_url("really-simple-captcha/tmp/" . $image) . '" alt="" /></div> 
     <div> 
      <input type="submit" value="'.$label_submit.'" name="send" id="cf_send" /> 
     </div> 

    </form>'; 

    if($sent == true) { 
     return $info; 
    } else { 
     return $info.$email_form; 
    } 
} 
add_shortcode('contact', 'wptuts_contact_form_sc'); 

// add plugins style to frontend 
function prize_game_contact_form() { 
wp_register_style('prize_game_contact_form', plugins_url('css/style.css',__FILE__)); 
wp_enqueue_style('prize_game_contact_form'); 
} 
add_action('wp_enqueue_scripts', 'prize_game_contact_form'); 

我沒加$captcha_instance->remove($prefix);到目前爲止,因爲它的工作原理並不像它應該。似乎每次我點擊提交按鈕時都會創建一個新單詞,並且函數$captcha_instance->check()會檢查新單詞是否等於輸入字段中的驗證碼文本,但這些單詞當然不匹配,因爲它已經是一個新單詞。我不明白爲什麼單擊提交按鈕時會創建一個新單詞。錯誤在哪裏?

回答

1

我相信問題是,你正在生成的函數運行一個新的$prefix各一次。因此,無法運行check()remove(),因爲表單上使用的$prefix是一個新值。

在您的表單中,使用正在使用的$prefix的值創建隱藏的輸入字段。所以這個值與表單一起發佈。當函數在表單發佈後第二次運行時,請使用發佈的$prefix值執行check()remove()。這應該做到這一點。

這裏是如何真正簡單的captcha插件連同我剛纔提到的隱藏的前綴字段集成到你的插件或主題的詳細文章:http://www.lost-in-code.com/platforms/wordpress/wordpress-plugins/wordpress-using-really-simple-captcha/

隨時讓我知道如果你需要任何幫助。

+1

Thx,沒有測試它,因爲這是一箇舊的項目,但我想這將工作。 – user2718671

1

你使用Wordpress嗎?不要試圖重新發明輪子。

下載一個免費的插件,如Contact Form 7其中包括一個Ajax驅動(不需要頁面刷新)Askimet垃圾郵件過濾器,外加CAPTCHA系統。

  • CAPTCHA的作品只有3-5年前的一半。

我奉勸大家的是:

  • 別看簡單,但找什麼工作。
+0

感謝您的意見。我已經知道聯繫表格7,事實上 - 這是一個很好的插件。但是我想創建自己的帶有特殊功能的插件,我只是覺得創建一個簡單的插件比修改複雜的聯繫人窗體7插件更容易。我甚至不知道是否允許修改cf7腳本,所以我不得不嘗試;) – user2718671

0

,如果你不使用WordPress,那麼你可能要檢查這個simple form & captcha idea - 如果你能搶出驗證碼部分就是驗證碼已成爲令人難以置信的複雜,在設計時,它需要做的一切簡直區分一個人類和非人類的大多數目的。

0

只有在您檢查用戶答案的​​正確性後,您必須創建變量$ word,$ prefix和$ image,並且必須通過隱藏的輸入字段傳遞$前綴值。我沒有檢查它,但是這個代碼現在應該工作:

function wptuts_get_the_ip() { 
    if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { 
     return $_SERVER["HTTP_X_FORWARDED_FOR"]; 
    } 
    elseif (isset($_SERVER["HTTP_CLIENT_IP"])) { 
     return $_SERVER["HTTP_CLIENT_IP"]; 
    } 
    else { 
     return $_SERVER["REMOTE_ADDR"]; 
    } 
} 
// short code function 
function wptuts_contact_form_sc($atts) {  
add_shortcode('contact', 'contact_form_shortcode'); 

    extract(shortcode_atts(array(
     "email" => get_bloginfo('admin_email'), 
     "subject" => '', 
     "label_name" => 'Your Name', 
     "label_email" => 'Your E-mail Address', 
     "label_subject" => 'Your Answer', 
     "label_captcha" => 'Captcha', 
     "label_submit" => 'Submit', 
     "error_empty" => 'Please fill in all the required fields.', 
     "error_noemail" => 'Please enter a valid e-mail address.', 
     "success" => 'Thanks for your e-mail! We\'ll get back to you as soon as we can.' 
    ), $atts)); 

    $captcha_instance = new ReallySimpleCaptcha(); //imports really simple captcha plugin 

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

     $error = false; 
     $required_fields = array("your_name", "email", "captcha", "subject"); 

     foreach ($_POST as $field => $value) { 
      if (get_magic_quotes_gpc()) { 
       $value = stripslashes($value); 
      } 
      $form_data[$field] = strip_tags($value); 
     } 

     foreach ($required_fields as $required_field) { 
      $value = trim($form_data[$required_field]); 
      if(empty($value)) { 
       $error = true; 
       $result = $error_empty; 
      } 
     } 

     if(!is_email($form_data['email'])) { 
      $error = true; 
      $result = $error_noemail; 
     } 

     if ($error == false) { 

      $captcha_text=$form_data['captcha']; 
      $captcha_prefix=$form_data['prefix']; 
      $correct = $captcha_instance->check($captcha_prefix, $captcha_text); 

      if($correct==TRUE){ 

       $email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject']; 
       $email_message = $form_data['captcha'] . "\n\nIP: " . wptuts_get_the_ip(); 
       $headers = "From: ".$form_data['your_name']." <".$form_data['email'].">\n"; 
       $headers .= "Content-Type: text/plain; charset=UTF-8\n"; 
       $headers .= "Content-Transfer-Encoding: 8bit\n"; 
       wp_mail($email, $email_subject, $email_message, $headers); 
       $result = $success; 
       $sent = true; 
      } 
     } 
    } 

    $word = $captcha_instance->generate_random_word();   
    $prefix = mt_rand(); 
    $image= $captcha_instance->generate_image($prefix, $word); 

    if($result != "") { 
     $info = '<div class="info">'.$result.'</div>';   
    } 

    $email_form = '<form class="contact-form" method="post" action="'.get_permalink().'"> 
     <div> 
      <label for="cf_name">'.$label_name.':</label> 
      <input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" /> 
     </div> 
     <div> 
      <label for="cf_email">'.$label_email.':</label> 
      <input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" /> 
     </div> 
     <div> 
      <label for="cf_subject">'.$label_subject.':</label> 
      <input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" /> 
     </div> 
     <div> 
      <label for="cf_message">'.$label_captcha.':</label> 
      <input type="text" name="captcha" id="cf_message" size="50" maxlength="50" value=""/> 
      <input type="hidden" name="prefix" id="prefix" value="'.$prefix.'"/> 
     </div> 
     <div id="captcha"><span id="captcha_text">Captcha:</span><img id="captcha_image" src="'.plugins_url("really-simple-captcha/tmp/" . $image) . '" alt="" /></div> 
     <div> 
      <input type="submit" value="'.$label_submit.'" name="send" id="cf_send" /> 
     </div> 

    </form>'; 

    if($sent == true) { 
     return $info; 
    } else { 
     return $info.$email_form; 
    } 
} 
add_shortcode('contact', 'wptuts_contact_form_sc'); 

// add plugins style to frontend 
function prize_game_contact_form() { 
wp_register_style('prize_game_contact_form', plugins_url('css/style.css',__FILE__)); 
wp_enqueue_style('prize_game_contact_form'); 
} 
add_action('wp_enqueue_scripts', 'prize_game_contact_form');