【问题标题】:Codeigniter: validation problemCodeigniter:验证问题
【发布时间】:2010-06-09 11:57:45
【问题描述】:

由于某种原因,用户可以使用任何密码登录,首先我以为我忘记检查密码但我没有......我只是找不到问题

这是模型:

/*#######################################################*/
    function validate()
/*#######################################################*/
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $q = $this->db->get('user_extra');

        if($q->num_rows() == 1):
            return true;
        else:
            return false;
        endif;
    }//end of function validate()

控制器

/*#######################################################*/
        function validate_credentials()
/*#######################################################*/
        {
            $this->load->model('membership_model');

            $this->load->library('form_validation');

            $this->form_validation->set_rules('username', 'Name', 'trim|required');
            $this->form_validation->set_rules('password', 'password', 'trim|required');
            if(!$this->membership_model->validate()):
                $this->form_validation->set_message('check_login', 'Login not correct, please try again.');
            endif;

            if($this->form_validation->run() == FALSE):
                $this->index();
            else:
                $this->membership_model->userinfo($this->input->post('username'));
                //should redirect to last view
                redirect($this->session->flashdata('redirect_url'));
            endif;
        }// end of validate_credentials()

【问题讨论】:

    标签: php authentication codeigniter login


    【解决方案1】:

    为什么不在验证字符串中创建回调函数?

    将此函数放在同一个控制器中

    /*##############PRIVATE FUNCTION WITHIN THE SAME CONTROLLER###########*/
        function validate($username,$password) //if you use php 5 i would set this method to  private one
    /*#######################################################*/
        {
            $this->db->where('username', $username);
            $this->db->where('password', md5($password));
            $q = $this->db->get('user_extra');
    
            if($q->num_rows() <= 1)
                return true;
            return false;
    
        }//end of function validate()
    

    还有规则线:

    $this->form_validation->set_rules('username', 'Name', 'trim|required|callback_validaion[password]');
    

    【讨论】:

    • callback_validaion[password] 在 set_rules 中,你将控制器函数命名为 validate(),它应该是 validation() 吗?无论如何我试过了,但你只传递密码,你确定它会找到用户名吗?除了您在控制器中添加了查询之外,它应该在模型中。但感谢您的回复
    • 哦,对不起我之前的评论,我现在明白你的意思了,我的错。
    【解决方案2】:

    为什么不在表单验证中使用 caalback?

    例如

    $this->form_validation->set_rules('password', 'Password', 'prep_for_form|required|xss_clean|callback_password_check');
    

    还有回调函数...

    function password_check($value) { /* call model and query database to get password */   if ($value == $password_from_model))    {       return TRUE;    }   else    {       $this->form_validation->set_message('password_check', 'You have not used the correct %s.');         return FALSE;   } }
    

    【讨论】:

    • 对不起,我不好。忘记在回调条件中添加等于!
    【解决方案3】:

    我尝试了回调函数,但由于某种原因它不起作用,所以我只是将下面的 if 语句移到了validation->run()的else语句中,这是代码:

    $this->form_validation->set_rules('username', 'Name', 'trim|required|xss_clean');
                $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean');
    
    
                if($this->form_validation->run() == FALSE):
                    redirect(base_url().'login/index/error');
                else:       
                    if($this->membership_model->validate()):
                        $this->membership_model->userinfo($this->input->post('username'));
                        //should redirect to last view
                        redirect('home/index');
                    else:
                        redirect(base_url().'login/index/error');
                    endif;
                endif;
    

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多