用户登录时留在仪表板中

时间:2016-08-01 19:39:57

标签: php codeigniter

即使用户编写localhost / storeLTE / login /然后呆在家中,如何在用户登录时仍留在仪表板中。但我的代码不起作用。

public function getAccess(){
            if ($this->session->set_userdata('username')) {
                redirect('home');
            }
            $username = $this->security->xss_clean($this->input->post('username'));
            $password = $this->security->xss_clean($this->input->post('password'));
            $array = $this->User_model->login($username,$password);
            if($array[0] == 0){
                echo 0;
            }else{
                $data_session = array(
                    'id' => $array[0]['iduser'],
                    'username' => $array[0]['username'],
                    'password' => $array[0]['password'],
                    'name' => $array[0]['name'],
                    'last_name' => $array[0]['last_name'],
                    'type' => $array[0]['idType'],
                    'logged_in' => TRUE
                );
                $this->session->set_userdata('log',$data_session);
            }
        } 

3 个答案:

答案 0 :(得分:2)

if ($this->session->set_userdata('username')) {

应该是

if ($this->session->userdata('username')) {

if ($this->session->userdata('username') !== NULL) {
//since NULL is returned if item is not found

Docs

答案 1 :(得分:1)

FYI

这不是在会议中存储密码的好兆头。最好存储nametypelogged_inid

在控制器中

function getAccess(){

    $this->load->library('session'); # load library here or in autoload.php

    if($this->session->userdata('logged_in') == TRUE) 
    {
        redirect('home');
    }
    else
    {
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        $result = $this->User_model->login($username,$password);
        if($result == FALSE)
        {
            echo 'Invalid Login';
        }
        else{
            $data_session = array(
                'id' => $result[0]['iduser'],
                'username' => $result[0]['username'],  # Better to remove
                'password' => $result[0]['password'], # Better to remove
                'name' => $result[0]['name'],
                'last_name' => $result[0]['last_name'],
                'type' => $result[0]['idType'],
                'logged_in' => TRUE
            );
            $this->session->set_userdata('log',$data_session);

            $this->load->view('home'); # Load the view
        }
    }

} 

在模型中

function login($username,$password)
{
    $query = $this->db->query("SELECT * FROM table name WHERE username = '$username' AND password = '$password'");
    $result = $query->result_array();

    if (count($result) > 1 || empty($result)) 
    {
        return FALSE;
    } 
    else {
        return $result;
    }       
}

答案 2 :(得分:0)

  if ($this->session->set_userdata('username')) {
                redirect('home');
            }

将此更改为

  if ($this->session->userdata('username') !='') {
                redirect('home');
            }