2012-04-06 88 views
0

我有一個簡單的聯繫表單與幾個必需的字段,它工作正常。我正在嘗試將reCaptcha添加到表單中以增加一些垃圾郵件保護功能。CodeIgniter reCaptcha和Form_validation類

我正確地在窗體上顯示reCaptcha,我安裝了我的鑰匙。在我的控制器中,我可以調用recaptcha_check_answer方法並獲取有效的響應。我已經正確安裝和配置了所有文件,因爲我可以使用recaptcha類中的方法並獲得很好的響應。

我的問題是,我想有一個無效的reCaptcha觸發一個CI form_validation錯誤,但我花了很多時間在這個,並不能讓它觸發。我對CI很新,所以如果這很簡單,請原諒我的無知。

我試圖在is_valid響應返回NULL時引發form_validation錯誤。如果NULL然後我試圖做到以下幾點:

$this->form_validation->set_message('recaptcha_response_field', 'reCaptcha', lang('recaptcha_incorrect_response')); 
echo form_error('recaptcha_response_field'); 

這裏的ReCaptcha API響應是什麼樣的有效和無效的驗證碼的:

Valid = stdClass Object ([0] => is_valid [1] => error [is_valid] => 1) 
Invalid = stdClass Object ([0] => is_valid [1] => error [is_valid] => [error] => incorrect-captcha-sol) 

這裏是我在我的控制器代碼:

class Contact extends CI_Controller 
{ 

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

public function index() 
{ 
    $this->load->library('recaptcha'); 
    $this->recaptcha->set_key ('MY_PUBLIC_KEY_HERE' ,'public'); 
    $this->recaptcha->set_key ('MY_PRIVATE_KEY_HERE' ,'private'); // For private key 

    // Is this a Form Submission? 
    if (!empty($_POST)) 
    { 

     // LOAD VALIDATION AND SET RULES 
     $this->load->library('form_validation'); 
     $this->load->helper('form'); 


     $this->form_validation->set_rules('first_name', 'First Name', 'required'); 
     $this->form_validation->set_rules('last_name', 'Last Name', 'required'); 
     $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
     $this->form_validation->set_rules('recaptcha_response_field', 'reCaptcha', 'required|recaptcha_check_answer'); 
     $this->_resp = $this->recaptcha->recaptcha_check_answer ($this->input->ip_address(), $this->input->post('recaptcha_challenge_field',true), $this->input->post('recaptcha_response_field',true)); 

     // If the reCaptcha doesnt match throw an form_valiation Error 
     if ($this->_resp->is_valid == '') 
     { 
      $this->form_validation->set_message('recaptcha_response_field', 'reCaptcha', lang('recaptcha_incorrect_response')); 
      echo form_error('recaptcha_response_field'); 
     } 



     if ($this->form_validation->run() !== FALSE) 
     { 
      // SUCCESS NO ERRORS - SEND EMAIL DATA AND RETURN TO HOME 
       $this->load->library('email'); // LOAD EMAIL LIBRARY 
       $config['protocol'] = 'sendmail'; 
       $config['mailpath'] = '/usr/sbin/sendmail'; 
       $config['charset'] = 'utf-8'; 
       $config['wordwrap'] = TRUE; 

       $this->email->initialize($config); 

       $this->email->from($this->input->post('email'), $this->input->post('first_name').' '.$this->input->post('last_name')); 
       $this->email->to('[email protected]'); 
       $this->email->cc(''); 
       $this->email->bcc(''); 

       $this->email->subject('Contact Email From Spider Design Website'); 
       $this->email->message(
        'First Name: '.$this->input->post('first_name')."\r\n". 
        'Last Name: '.$this->input->post('last_name')."\r\n". 
        'Company: '.$this->input->post('company')."\r\n". 
        'Location: '.$this->input->post('location')."\r\n"."\r\n". 
        'I am interested in learning: '."\r\n"."\r\n". 
        'Information Architecture: '.$this->input->post('information_architecture')."\r\n". 
        'Web Design: '.$this->input->post('web_design')."\r\n". 
        'Graphic Design: '.$this->input->post('graphic_design')."\r\n". 
        'Email Marketing: '.$this->input->post('email_marketing')."\r\n". 
        'Social Media: '.$this->input->post('social_media')."\r\n". 
        'Content Management: '.$this->input->post('content_management')."\r\n". 
        'Search Engine Optimization: '.$this->input->post('seo')."\r\n". 
        'Web Traffic Analytics: '.$this->input->post('wta')."\r\n"."\r\n". 
        'Preferred Method of Contact: '."\r\n". 
        'Phone: '.$this->input->post('phone')."\r\n". 
        'Email: '.$this->input->post('email')."\r\n"."\r\n". 
        'Comments: '."\r\n". 
        $this->input->post('comments')    
        ); 

      //$this->email->send(); 
      //$this->load->helper('url'); 
      //redirect('http://sdi.myspiderdesign.com/fuel'); 
     } 
    } 

     // Not a form submission, load view 
     $this->load->helper('form'); 
     $this->load->view('include/spider_header'); 
     $this->load->view('contact'); // Content Area 
     $this->load->view('include/spider_footer'); // Footer 
    } 
} 

我已將表單底部的重定向和聯繫電子郵件註釋掉,直到我可以排除此問題。

在此先感謝您的幫助!

回答

0

我使用了form_validation庫中的callback_函數。在進行API調用的控制器內部設置一個附加函數,如果驗證碼沒有通過,它會拋出一個validation_error。

相關問題