0

我有一个简单的联系表格,其中包含几个必填字段,效果很好。我正在尝试将 reCaptcha 添加到表单中以增加一些垃圾邮件保护。

我在表单上正确显示了 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('jassen.michaels@gmail.com');
              $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
   }
}

我已经注释掉了表单底部的重定向和联系电子邮件,直到我可以解决这个问题。

提前感谢您的所有帮助!

4

1 回答 1

0

我在 form_validation 库中使用了 callback_ 函数。在进行 API 调用的控制器内设置一个附加函数,如果验证码未通过,它将引发验证错误。

于 2012-04-07T21:42:14.287 回答