2017-01-09 6 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']のように、私はテンプレートを使用しているので、各ビューの頭にタイトルのプレースホルダとして使用します。

私は何か間違っていますか?なぜ私はprint_r MY_Controllerからのセッションデータを返すだけ '1'を返します。

私はガイドを見てきましたが、それらのほとんどが古く、過去のバージョンでは廃止予定のものを使用しています。親切に指摘すれば、丁寧できれいなコーディングを練習したいと思います。

答えて

0

は、あなたのアプローチは、私はあなたが使用しているCIバージョンはよく分からないが、あなたは、セッションライブラリをAUTOLOAD場合は、必ず結果になるだろう(そのように動作しません。ここをクリック)

余分なキーを設定する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'); 
    } 
} 

あなたはtrueまたはfalseを返し、正しい値またはNULLのいずれかを取得します。詳細については

あなたはスーパー返事が遅れて申し訳ありません、ここで https://codeigniter.com/userguide3/libraries/sessions.html#CI_Session::userdata

+0

を見てみることができます。 別のキーを設定するとどういう意味ですか? セッションで別のarray_keyを設定しようとしていますか? 'is_logged_in'を1または0、またはtrueまたはfalseに設定しますか?あなたのモデルを見せてくれる正確な答えを得るために –

+0

;) – sintakonte

0

すべてのあなたのセッションデータを印刷したい場合は1または0を返します。この

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

この部分

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

を使用します。ユーザデータは、__ci_last_regenerateと呼ばれる少なくとも1つのキーを持つ配列を返すため

関連する問題