Php 用户登录时,请留在仪表板中

Php 用户登录时,请留在仪表板中,php,codeigniter,Php,Codeigniter,即使用户写localhost/storeLTE/login/然后呆在家里,当用户登录时,我如何保持在仪表板中。但是我的代码不起作用 public function getAccess(){ if ($this->session->set_userdata('username')) { redirect('home'); } $username = $this->securit

即使用户写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);
            }
        } 
应该是

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

应该是

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

.

Its不是在会话中存储密码的好迹象。最好存储
名称
类型
登录
id


在控制器中

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

Its不是在会话中存储密码的好迹象。最好存储
名称
类型
登录
id


在控制器中

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;
    }       
}
将此更改为

  if ($this->session->userdata('username') !='') {
                redirect('home');
            }
将此更改为

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

或者,我如何获取if-else if-else的代码?同样的方法还是我必须改变很多=?我不确定你的代码的其余部分,但我可以建议你编写一个名为“ion auth”的好的身份验证。或者我如何为if-else if-else插入我的代码?同样的方法还是我必须做很多修改=?我不确定你的代码的其余部分,但我可以建议你使用编写良好的身份验证,称为“ion auth”。如果你遇到未设置的问题。使用$this->session->sess_destroy();如果您遇到未设置的问题。使用$this->session->sess_destroy();您正在设置用户名,而不是检查用户名。您正在设置用户名,而不是检查用户名。