簡體   English   中英

實施Codeigniter登錄的最佳方法

[英]best way to implement Codeigniter login

這是我的登錄控制器索引函數:

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);

無需加載視圖文件,您必須將其重定向到索引功能。 並使用Session flash數據來實現此目的。

喜歡,

調節器

$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');
}?>

您的控制器

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
        }
    }
}

調節器

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');

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM