2016-08-01 6 views
0

ユーザーがlocalhost/storeLTE/login/homeに書き込んでもユーザーがログに記録されたとき、どうすればダッシュボードに留まることができますか?私のコードはうまくいきません。ユーザーがログに記録されたときにダッシュボードに留まる

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の場合、私のコードをどのように入れ替えることができますか?同じように、あるいは私は多くを変えなければならない=? –

+0

残りのコードについてはわかりませんが、 "ion auth"と呼ばれるよく書かれた認証を提案することができます。 – Tpojka

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; 
    }  
} 
0
if ($this->session->set_userdata('username')) { 
       redirect('home'); 
      } 

変更本で

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

ユーザ名を確認する代わりに、ユーザ名を設定しています。 –

関連する問題