【问题标题】:Codeigniter ReCaptcha and form validation configCodeigniter ReCaptcha 和表单验证配置
【发布时间】:2013-06-04 01:46:55
【问题描述】:

下面是我的代码的 sn-p:

auth.php

    // Login the user
public function login(){

    $view_data = new stdClass;
    // Form Data
    $view_data->login_form = $this->auth_lib->get_login_form_data();
    $view_data->reg_form = $this->auth_lib->get_reg_form_data();
    $view_data->login_recaptcha = '';
    $view_data->reg_recaptcha = '';

    // Set an attempt
    $this->auth_model->set_form_submit('Login');

    // Get number of attemps
    $login_count = $this->auth_model->get_form_submit_count('Login', 3600);

    if($login_count >= 3){
        // 3 or more attempts

        $view_data->login_recaptcha = recaptcha();
        $privkey = $this->config->item('recaptcha_private_key');
        $remoteip = $_SERVER['REMOTE_ADDR'];
        $challenge = $this->input->post('recaptcha_challenge_field');
        $response = $this->input->post('recaptcha_response_field');

        $resp = recaptcha_check_answer($privkey, $remoteip, $challenge, $response);
        if($resp->is_valid !== TRUE){
            $this->form_validation->set_rules('recaptcha_response_field', 'Recaptcha Response Field', 'required');
            $this->form_validation->set_rules('recaptcha_challenge_field', 'Recaptcha Challenge Field', 'required|matches[recaptcha_response_field]');
        }

    }

    if($this->form_validation->run() == FALSE){
        // Not valid input
        $template_data = $this->template->load('auth/login', $view_data);
        $this->load->view($template_data->template, $template_data);    

    }else{
        // Valid input

    }

}

form_validation.php

    $config = array(
            'auth/login' => array(
                    array(
                    'field' => 'login_username',
                    'label' => 'Username',
                    'rules' => 'required|min_length[4]|max_length[12]|'
                    ),
                    array(
                    'field' => 'login_password',
                    'label' => 'Password',
                    'rules' => 'required|min_length[6]|max_length[16]'
                    ),              
            ),
            'auth/register' => array(
                    array(
                    'field' => 'reg_username',
                    'label' => 'Username',
                    'rules' => 'required|min_length[4]|max_length[12]|'
                    ),
                    array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'required|min_length[3]|valid_email|'
                    ),
                    array(
                    'field' => 'email_again',
                    'label' => 'Email Again',
                    'rules' => 'required|min_length[3]|valid_email|matches[email]'
                    ),
                    array(
                    'field' => 'reg_password',
                    'label' => 'Password',
                    'rules' => 'required|min_length[6]|max_length[16]'
                    ),              
            ),
    );

一切正常,我在配置文件“form_validation.php”中设置了表单验证规则,并且在调用相应的控制器/方法时自动使用如 CI 手册中所示的标记为“控制器/方法”。

如果登录计数大于或等于定义的数量(在本例中为 3),则显示 ReCaptcha 表单。

问题是这样的,如果用户输入了错误的 ReCaptcha 并且未能在其他表单字段中输入所需的信息,那么只会显示 ReCaptcha 错误。如果用户正确输入了 ReCaptcha 字段并且未能在其他字段中输入信息,则会显示这些错误。

如果像这样在控制器中设置验证规则:

$this->form_validation->set_rules('login_username', 'Username', 'required');    

然后,如果它存在于 ReCaptcha 错误旁边,如果它像您预期的那样存在,则会显示该错误。

我希望显示任何或所有存在的错误,我怎样才能做到这一点而不必在控制器中设置我的验证?

我只是缺少一些简单的东西吗?谢谢!

【问题讨论】:

    标签: codeigniter validation recaptcha


    【解决方案1】:

    在我找到更好的解决方案允许我使用 form_validation.php 配置文件之前,我决定在下面使用这种方法。

    public function register(){
    
            if($login_count >= 3){
            // 3 or more attempts
    
            $view_data->reg_recaptcha = recaptcha();
            $privkey = $this->config->item('recaptcha_private_key');
            $remoteip = $_SERVER['REMOTE_ADDR'];
            $challenge = $this->input->post('recaptcha_challenge_field');
            $response = $this->input->post('recaptcha_response_field');
    
            $resp = recaptcha_check_answer($privkey, $remoteip, $challenge, $response);
    
        }
    
        if( isset($resp->is_valid) && ($resp->is_valid !== TRUE) ){
    
            $this->set_validation_rules('register', true);      
        }else{
            $this->set_validation_rules('register', false);
        }       
    
        if($this->form_validation->run() == FALSE){
            // Not valid input
            $template_data = $this->template->load('auth/register', $view_data);
            $this->load->view($template_data->template, $template_data);    
    
        }else{
            // Valid input
    
        }
    }
    
    
    public function set_validation_rules($form = '', $recaptcha = FALSE){
    
        if($recaptcha !== FALSE){
            $this->form_validation->set_rules('recaptcha_response_field', 'Recaptcha Response Field', 'required');
            $this->form_validation->set_rules('recaptcha_challenge_field', 'Recaptcha Challenge Field', 'required|matches[recaptcha_response_field]');
        }
    
        if($form === 'login'){
            $this->form_validation->set_rules('login_username', 'Username', 'required');
            $this->form_validation->set_rules('login_password', 'Password', 'required');
        }
    
        if($form === 'register'){
            $this->form_validation->set_rules('reg_username', 'Username', 'required|min_length[4]|max_length[12]');
            $this->form_validation->set_rules('email', 'Email', 'required|min_length[3]|valid_email');
            $this->form_validation->set_rules('email_again', 'Email Again', 'required|min_length[3]|valid_email|matches[email]');
            $this->form_validation->set_rules('reg_password', 'Password', 'required|min_length[6]|max_length[16]');         
        }
    

    这不是我想做的,如上所述,我想将我的验证保存在配置文件中。因此,如果您有更好的解决方案,请分享!在那之前,这将是可行的。

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2013-04-10
      相关资源
      最近更新 更多