2016-08-01 101 views
0

即使用户编写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); 
      } 
     } 
+0

如果遇到未设置的问题。使用$ this-> session-> sess_destroy(); –

回答

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

+0

或我如何内嵌我的代码if-else if -else?同样的方式,还是我必须改变很多=? –

+0

我不确定关于您的其他代码,但我可以建议您写好认证,称为“离子认证”。 – Tpojka

1

FYI

它是不是在会议蓄光密码的一个好兆头。它更好地存储name,type,logged_in,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; 
    }  
} 
0
if ($this->session->set_userdata('username')) { 
       redirect('home'); 
      } 

改变这对

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

您正在设置用户名,而不是检查用户名。 –