首页 文章

Codeigniter ReCaptcha和表单验证配置

提问于
浏览
1

以下是我的代码片段:

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错误旁边,则会显示错误,如果它存在,就像您期望的那样 .

我希望显示存在的任何或所有错误,如何在控制器中设置验证时如何执行此操作?

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

1 回答

  • 0

    目前,直到我找到一个更好的解决方案,允许我使用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]');         
        }
    

    这不是我想要做的,如上所述,我想在配置文件中保留我的验证 . 所以如果你有更好的解决方案请分享!在此之前,这样做 .

    谢谢!

相关问题