2017-01-09 37 views
0

我有這個在我的控制器命名的網頁(我使用它作爲一個終端分支到所有視圖)會話數據CI3基本控制器

class pages extends MY_Controller { 
    public function verification(){ 

    $session_data = $this->is_logged_in(); 
    print_r($session_data); 
     if($this->is_logged_in()){ 


     } 
    } 
} 

正如你所看到的,我有一個行「的print_r」 session_data,我得到了一些結果,問題是它總是隻返回'1'。

這是我的基礎控制器上的功能。

class MY_Controller extends CI_Controller { 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function is_logged_in() 
    { 
     $user = $this->session->userdata(); 
     return isset($user); 
    } 
} 

這是在我的身份驗證器控制器上創建會話數據後登錄的代碼塊。

Class user_authentication extends MY_Controller { 

public function register(){ 
    $this->form_validation->set_rules('username', 'Username','required|trim|min_length[8]|max_length[24]|is_unique[user.username]', 
    array('required' => 'You have not provided %s.','is_unique' => 'This %s already exists.')); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[16]'); 
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[6]|max_length[30]|is_unique[user.email]', 
    array('required' => 'You have not provided %s. ','is_unique' => 'This %s already exists.')); 
    $this->form_validation->set_rules('type', 'Account Type', 'trim|required|min_length[1]|max_length[1]', 
    array('required' => 'please select: 1 - normal, 2 - trainer')); 
    $this->form_validation->set_error_delimiters('', ''); 
     if ($this->form_validation->run() == FALSE){ 
      $this->load->view('templates/header'); 
      $this->load->view('templates/navigation'); 
      $this->load->view('pages/login'); 
      $this->load->view('templates/footer'); 
     }else{ 
      $data = array('username' => $this->input->post('username'), 
          'password' => $this->input->post('password'), 
          'email' => $this->input->post('email'), 
          'type' => $this->input->post('type')); 
      $result = $this->Account_model->create_account($data); 
       if($result == true){ 
        $data['message_display'] = 'Registration Successful'; 
        $this->load->view('templates/header'); 
        $this->load->view('templates/navigation'); 
        $this->load->view('pages/homepage',$data); 
        $this->load->view('templates/footer');  
       }else{ 
        $data['message_display'] = 'Username and/or Email already exists'; 
        $this->load->view('templates/header'); 
        $this->load->view('templates/navigation'); 
        $this->load->view('pages/login',$data); 
        $this->load->view('templates/footer');} 
     } 
} 

public function login(){ 
$this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[24]'); 
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[16]'); 
    if($this->form_validation->run() == FALSE){ 
     $this->load->view('templates/header'); 
     $this->load->view('templates/navigation'); 
     $this->load->view('pages/login'); 
     $this->load->view('templates/footer');} 
    else{ 
     $data = array('username' => $this->input->post('username'), 'password' => $this->input->post('password')); 
     $result = $this->Account_model->login_account($data); 
     $session_data = array('username' => $result['username'], 'email' => $result['email'], 'type' => $result['type']); 
     $this->session->set_userdata('isloggedin',$session_data); 
     $data['title'] = 'home'; 
     $this->load->view('templates/header'); 
     $this->load->view('templates/navigation'); 
     $this->load->view('pages/home'); 
     $this->load->view('templates/footer');} 
} 

現在有些東西基本上沒用,但我把它們放在那裏以備將來使用。比如$ data ['title']會使用它作爲每個視圖頭部標題的佔位符,因爲我使用的是模板。

我做錯了什麼?爲什麼當我從MY_Controller打印會話數據時,它只返回'1'。

我一直在看導遊,但其中大多數都過時了,我可能使用過去版本中的棄用東西,如果這樣指出,我想練習編碼整齊和乾淨。

回答

0

,因爲用戶數據返回一個名爲__ci_last_regenerate至少一個鍵的數組你的方法是行不通的那樣(我不知道你使用的CI版本,但是如果你自動載入會議庫,你會總是得到一個結果這裏)

設置一個額外的鍵isLoggedin,你會沒事的。

您的登錄功能:

public function login() 
{ 
    $this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[24]'); 
    $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[16]'); 
    if($this->form_validation->run() == FALSE) 
    { 
     $this->load->view('templates/header'); 
     $this->load->view('templates/navigation'); 
     $this->load->view('pages/login'); 
     $this->load->view('templates/footer');} 
    else 
    { 
     $data = array('username' => $this->input->post('username'), 'password' => $this->input->post('password')); 
     $result = $this->Account_model->login_account($data); 

     if ($result) 
     { 
      $session_data = array('username' => $result['username'], 'email' => $result['email'], 'type' => $result['type'], 'isLoggedin' => true); 
      $this->session->set_userdata($session_data); 

      $data['title'] = 'home'; 
      $this->load->view('templates/header'); 
      $this->load->view('templates/navigation'); 
      $this->load->view('pages/home'); 
      $this->load->view('templates/footer'); 

     } 

    } 
} 

和控制器

class MY_Controller extends CI_Controller { 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function is_logged_in() 
    { 
     return $this->session->userdata('isLoggedin'); 
    } 
} 

用這種方法你無論是正確的值或NULL返回true或false;

欲瞭解更多信息,可以到這裏看看 https://codeigniter.com/userguide3/libraries/sessions.html#CI_Session::userdata

+0

對不起的答覆超級晚。 你是什麼意思設置另一個關鍵? 你的意思是在我的會話中設置另一個array_key?作爲'is_logged_in'爲1或0或真或假? –

+0

爲了給你一個準確的答案,你必須告訴我你的模型;) – sintakonte

0

如果你想打印所有的會話數據使用

print_r($this->session->all_userdata()); 

,這部分

public function is_logged_in() 
    { 
     $user = $this->session->userdata(); 
     return isset($user); 
    } 

將返回1或0;