2017-04-15 62 views
0

在數據庫中我有用戶名,密碼,身份證,管理等,但我的管理員值不顯示。爲什麼? http://prntscr.com/ewuiz9 http://prntscr.com/ewuj4o會話userdata不顯示管理值

 // Get username 
     $username = $this->input->post('username'); 
     // Get and encrypt the password 
     $password = md5($this->input->post('password')); 

     // Login user 
     $user_id = $this->user_model->login($username, $password); 

     if($user_id){ 
      // Create session 


      $user_data = array(
       'admin' => $admin, 
       'user_id' => $user_id, 
       'username' => $username, 
       'logged_in' => true 
      ); 

      $this->session->set_userdata('logged_in', $user_data); 

      // Set message 
      $this->session->set_flashdata('user_loggedin', 'You are now logged in'); 

      redirect('posts'); 
     } else { 
      // Set message 
      $this->session->set_flashdata('login_failed', 'Login is invalid'); 

      redirect('users/login'); 
     } 
    } 
} 
+0

提示:不要使用MD5口令非常不安全的。使用PHP密碼哈希http://php.net/manual/en/function.password-hash.php並驗證http://php.net/manual/en/function.password-verify.php和https:// www.codeigniter.com/user_guide/general/compatibility_functions.html#password_hash – user4419336

回答

0

因爲你已經把它像

$user_data = array(
    'admin' => $admin, 
    'user_id' => $user_id, 
    'username' => $username, 
    'logged_in' => true 
); 

$this->session->set_userdata('logged_in', $user_data); 

您可以像另一個控制器上訪問它。

$userdata = $this->session->userdata('logged_in'); 

echo $userdata['username']; 

如果你只需要要訪問像

echo $this->session->userdata('username');

然後你只需要設置它像

$user_data = array(
    'admin' => $admin, 
    'user_id' => $user_id, 
    'username' => $username, 
    'logged_in' => true 
); 

$this->session->set_userdata($user_data); 

確保您已經自動加載會話可以有超過網站訪問它

的config/autoload.php

$autoload['libraries'] = array('session'); 

在config.php文件,確保您已設置會話路徑和會話文件夾的權限0700

$config['sess_driver'] = 'files'; 
$config['sess_cookie_name'] = 'ci_session'; 
$config['sess_expiration'] = 7200; 
$config['sess_save_path'] = APPPATH . 'cache/session/'; 
$config['sess_match_ip'] = FALSE; 
$config['sess_time_to_update'] = 300; 
$config['sess_regenerate_destroy'] = FALSE; 
0

謝謝。 在模型中,我發現:

// Log user in 
    public function login($username, $password){ 
     // Validate 
     $this->db->where('username', $username); 
     $this->db->where('password', $password); 

     $result = $this->db->get('users'); 

     if($result->num_rows() == 1){ 
      return $result->row(0)->id; 
     } else { 
      return false; 
     } 
    } 

我更改爲:

// Log user in 
    function login($username, $password){ 
     $this->db->select('id,admin,username,password,email'); 
     $this->db->from('users'); 
     $this->db->where('username', $username); 
     $this->db->where('password', $password); 
     $query = $this->db->get(); 
     if($query->num_rows()==1){ 
      return $query->result(); 
     } else{ 
      return false; 
     } 
    }