2012-05-08 44 views
2

當我使用codeIgniter和ReCaptcha時,這是一個奇怪的行爲。ReCaptcha無效+ CodeIgniter表單助手=表單數據丟失

我用ReCaptcha作爲一個簡單的應用程序/幫手。我所做的唯一的變化是重命名API「recaptcha_helper.php」我加入

if(!defined('RECAPTCHA")){ 
    define('RECAPTCHA',true); 
    [API code] 
} 

當發佈數據到我的控制器則結果並不如預期。

ReCaptcha valid/form valid = works fine! 
ReCaptcha valid/form not valid = works fine! 
ReCaptcha not valid/form valid = all form data lost 
ReCaptcha not valid/form not valid = all form data lost + validation lost 

此外,我使用「set_value('input_name')」像我所做的所有其他網站。它曾經像一個魅力工作,直到今天,當我把一個形式的recaptcha。

這裏是控制器:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Register extends MY_Controller { 


    var $form_rules = array( 
         array(
          'field' => 'user_first_name', 
          'label' => 'Prénom ', 
          'rules' => 'trim|required|max_lenght[50]|xss_clean'), 
         array(
          'field' => 'user_last_name', 
          'label' => 'Nom ', 
          'rules' => 'trim|required|max_lenght[50]|xss_clean'), 
         array(
          'field' => 'user_email', 
          'label' => 'Courriel ', 
          'rules' => 'trim|required|max_lenght[100]|valid_email|xss_clean'), 
         array(
          'field' => 'user_email_confirm', 
          'label' => 'Confirmation du courriel ', 
          'rules' => 'trim|required|max_lenght[100]|matches[user_email]|xss_clean')); 

    public function __construct(){ 
     parent::__construct(); 
    } 
    public function register(){ 

     $this->load->library('form_validation'); 
     $this->form_validation->set_rules($this->form_rules); 

     // RECAPTCHA STUFF 
     $this->load->helper('recaptcha'); 
     $publickey = "****"; 
     $privatekey = "****"; 
     # the response from reCAPTCHA 
     $resp = null; 
     # the error code from reCAPTCHA, if any 
     $error = null; 

     if(isset($_POST) && count($_POST)>0){ 
      $resp = recaptcha_check_answer ($privatekey, 
              $_SERVER["REMOTE_ADDR"], 
              $_POST["recaptcha_challenge_field"], 
              $_POST["recaptcha_response_field"]); 
      if ($resp->is_valid) { 
       if($this->form_validation->run()){ 
        $new_data = array( 'user_first_name' => $_POST['user_first_name'], 
             'user_last_name' => $_POST['user_last_name'], 
             'user_email' => $_POST['user_email']); 
        $this->db->insert('user', $new_data); 
        $new_user_id = $this->db->insert_id(); 
        $this->load->view('header'); 
        $this->load->view('sent'); 
        $this->load->view('footer'); 
        return; 
       } 
      }else{ 
       $error = $resp->error; 
      } 
      $data = $_POST; 
     } 

     $data['recaptcha'] = recaptcha_get_html($publickey, $error); 

     $this->load->view('header'); 
     $this->load->view('inscription_form', $data); 
     $this->load->view('footer'); 
    } 
} 

這裏是形式

<div id="inscription">   
    <?php echo form_open(); ?> 
     <p> 
      <label for="user_first_name">Pr&eacute;nom *</label> 
      <input type="text" name="user_first_name" value="<?php echo set_value('user_first_name'); ?>" maxlength="50" /> 
      <?php echo form_error('user_first_name'); ?> 
     </p> 
     <p class="clear"> 
      <label for="user_last_name">Nom *</label> 
      <input type="text" name="user_last_name" value="<?php echo set_value('user_last_name'); ?>" maxlength="50" /> 
      <?php echo form_error('user_last_name'); ?> 
     </p> 
     <p class="clear"> 
      <label for="user_email">Courriel *</label> 
      <input type="text" name="user_email" value="<?php echo set_value('user_email'); ?>" maxlength="100" /> 
      <?php echo form_error('user_email'); ?> 
     </p> 
     <p class="clear"> 
      <label for="user_email_confirm">Confirmation du courriel *</label> 
      <input type="text" name="user_email_confirm" value="<?php echo set_value('user_email_confirm'); ?>" maxlength="100" /> 
      <?php echo form_error('user_email_confirm'); ?> 
     </p> 
     <div class="clear"></div> 
     <div><?php echo $recaptcha; ?></div> 
     <p><input type="submit" value="Envoyer"/></p> 
     <p class="clear">* Tous les champs de ce formulaire sont requis.</p> 
    </form> 
</div> 

任何想法,爲什麼出現這種情況?

+0

另外我使用驗證碼1.11版 – Simmoniz

+0

您需要發佈您的查看+控制器表格,看看發生了什麼 – Laurence

+0

當然。我在問題中添加了代碼。謝謝 – Simmoniz

回答

2

爲了重新填寫表格必須運行form_validation命令。

但是,你的問題是,在你的代碼中,如果reCaptcha失敗,你永遠不會運行驗證,因此沒有什麼可以再次填充表單(因爲它實際上從未真正進入驗證過程)。

因此,改變你的代碼,並首先運行表單驗證。如果表單驗證返回true,則檢查驗證碼。

if(isset($_POST) && count($_POST)>0){ 
     $resp = recaptcha_check_answer ($privatekey, 
             $_SERVER["REMOTE_ADDR"], 
             $_POST["recaptcha_challenge_field"], 
             $_POST["recaptcha_response_field"]); 
     if($this->form_validation->run()) 
      { 
      if ($resp->is_valid) 
      { 
       $new_data = array( 'user_first_name' => $_POST['user_first_name'], 
            'user_last_name' => $_POST['user_last_name'], 
            'user_email' => $_POST['user_email']); 
       $this->db->insert('user', $new_data); 
       $new_user_id = $this->db->insert_id(); 
       $this->load->view('header'); 
       $this->load->view('sent'); 
       $this->load->view('footer'); 
       return; 
      } 
      else 
      { 
        $error = $resp->error; 
      } 
     } 
     $data = $_POST; 
    } 

一兩件事 - 提高你的代碼 - 讓您的驗證碼($ resp-> is_valid)回調方法,而不是 - 那麼你可以實際運行驗證碼的形式驗證的一部分,而不是一個單獨的方法。

See here for callbacks

public function _recaptcha_check($str) 
    { 
       $resp = recaptcha_check_answer ($str, 
            $_SERVER["REMOTE_ADDR"], 
            $_POST["recaptcha_challenge_field"], 
            $_POST["recaptcha_response_field"]); 
      if (! $resp->is_valid) 
      { 
      $this->form_validation->set_message('_recaptcha_check', 'Your reCaptcha was wrong!'); 
      return FALSE; 
     } 
     else 
     { 
      return TRUE; 
     } 
    } 

,並設置這個你設置(回調名稱注意雙「_」)的規則

array(
          'field' => 'recaptcha_response_field', 
          'label' => 'Recaptcha', 
          'rules' => 'required|callback__recaptcha_check'), 
+0

這終於不是一個錯誤,而是一個代碼14:P當驗證碼無效時,驗證不會運行。我還添加了recaptcha驗證作爲我的form_validation的一部分。感謝Laurencei你的明確答案! – Simmoniz

+0

爲了記錄,在回調示例中對'recaptcha_get_answer'的調用是錯誤的 - '$ str'應該是最後一個參數,因爲它是用戶輸入,您應該繼續使用您的私鑰作爲第一個參數。那就完美了!要在視圖中訪問與驗證碼相關的錯誤消息,只需使用'form_error('recaptcha_response_field');'。 – neemzy