实现Codeigniter登录的最佳方式

时间:2016-02-22 07:15:19

标签: php codeigniter

这是我的登录控制器索引功能:

public function index() {

    $data['title'] = 'Login';
    $this->load->view('login_form', $data);
}

然后是我的validate_credentials函数:

public function validate_credentials() {

    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run() == FALSE) {
        $this->index();
    } else {
        $this->load->model('user_model');
        if ($this->user_model->checkUserAccess($this->input->post('username'))) {           // checks if user has access to the site
            if ($this->ldapAuth()) {                                                        // checks if successful authentication with LDAP server
                $email = $this->user_model->getEmail($this->input->post('username'));
                $gpcid = $this->user_model->getUserGPC($this->input->post('username'));
                $this->user_model->updateTimestamp('users', 'DateLastLogin', 'UserID', $this->input->post('username'));
                $data = array (
                    'username' => $this->input->post('username'),
                    'gpcid' => $gpcid,
                    'email' => $email,
                    'is_logged_in' => true 
                    );
                $this->session->set_userdata($data);
                redirect('profile');
            } else {                                                                        // unsuccessful login
                $error = "Username and/or Password is incorrect";
                $data['title'] = 'Login';
                $data['message'] = $error;
                $this->load->view('login_form', $data);
            }
        } else {
            $error = "You do not have access to the site";
            $data['title'] = 'Login';
            $data['message'] = $error;
            $this->load->view('login_form', $data);
        }
    }
}

有没有办法只调用索引函数并传递错误信息而不是多次加载视图?

$data['title'] = 'Login';
$data['message'] = $error;
$this->load->view('login_form', $data);

3 个答案:

答案 0 :(得分:0)

您必须将其重定向到索引函数,而不是加载视图文件。并使用Session flash data来实现此目的。

喜欢,

<强>控制器

$this->session->set_flashdata( 'error', 'Username and/or Password is incorrect' );
redirect('index');

在login_form.php视图文件中打印消息

查看(login_form.php)

<?php  if($this->session->flashdata('error')){
       echo $this->session->flashdata('message');
}?>

答案 1 :(得分:0)

您的控制器

public function validate_credentials() {

    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run() == FALSE) {
        $this->index();
    } else {
        $this->load->model('user_model');
        if ($this->user_model->checkUserAccess($this->input->post('username'))) {           // checks if user has access to the site
            if ($this->ldapAuth()) {                                                        // checks if successful authentication with LDAP server
                $email = $this->user_model->getEmail($this->input->post('username'));
                $gpcid = $this->user_model->getUserGPC($this->input->post('username'));
                $this->user_model->updateTimestamp('users', 'DateLastLogin', 'UserID', $this->input->post('username'));
                $data = array (
                    'username' => $this->input->post('username'),
                    'gpcid' => $gpcid,
                    'email' => $email,
                    'is_logged_in' => true 
                    );
                $this->session->set_flashdata($data);
                redirect('profile');
            } else {   

                    $userdata = array (
                    'error' =>'Username and/or Password is incorrect',
                    );
                $this->session->set_flashdata($userdata);
                redirect('redirect url here'); // here is your url for index

            }
        } else {

            $userdata = array (
                    'error' =>'You do not have access to the site',
                    );
             $this->session->set_flashdata($userdata);
             redirect('redirect url here'); // here is your url for index
        }
    }
}

答案 2 :(得分:0)

控制器

public function loginaction() {

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

    $a = $this->input->post('email');
    $b = trim($this->input->post('password'));
    $b1 = md5($b);
    $c = 1;

    $data = $this->userdata->userlogin($a, $b1, $c);

    if($data) {

        echo true;

        foreach ($data as $login) {
            $uid = $this->session->set_userdata('logId', $login->usr_id);                                
        } 

    } else {

        echo "Invalid username or password..!!!!";
        $a = $this->input->post('email');

    }

}