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 呼び出しを行ったコントローラー内に追加の関数をセットアップします。captcha が渡されなかった場合は、validation_error がスローされます。

于 2012-04-07T21:42:14.287 に答える