2016-08-01 10 views
0

Wie kann ich im Dashboard bleiben, wenn der Benutzer angemeldet ist, obwohl wenn der Benutzer localhost schreiben/storeLTE/Login/dann zu Hause bleiben. aber mein Code funktioniert nicht.im Dashboard bleiben, wenn der Benutzer eingeloggt ist

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

Wenn Sie ein Problem mit Unset trafen. Verwende $ this-> session-> sess_destroy(); –

Antwort

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

sollte

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

oder

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

Docs sein.

+0

oder wie kann ich meinen Code für if-else infrage stellen wenn -else? genauso oder muss ich viel ändern =? –

+0

Ich bin mir nicht sicher in Bezug auf den Rest Ihres Codes, aber ich könnte Ihnen eine gut geschriebene Authentifizierung namens "ion auth" vorschlagen. – Tpojka

1

FYI

Sein ist kein gutes Zeichen für das Kennwort in der Sitzung zu speichern. Es ist besser zu speichern name, type, logged_in, id.


Kontroller

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

} 

In Modell

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

Änderung dieser zu

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

Sie setzen den Benutzernamen statt und überprüfen den Benutzernamen. –