2017-08-12 72 views
0

如何在我的所有控制器頁面中傳遞登錄會話用戶數據: 我嘗試了很多代碼,但請注意處理任何事情。請任何人都可以幫助我。 還加載Session庫會話。它得到錯誤codeigniter如何在所有控制器中傳遞登錄會話

Severity: Notice: Undefined variable: session_data 

Filename: controllers/Banner.php 

Line Number: 11 

Backtrace: 

File: C:\xampp\htdocs\aapkavyavahar\admin\application\controllers\Banner.php 
Line: 11 
Function: _error_handler 

File: C:\xampp\htdocs\aapkavyavahar\admin\index.php 
Line: 315 
Function: require_once 

我的登錄位指示:

function login_validation() 
    { 

     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('username', 'Username', 'required'); 
     $this->form_validation->set_rules('password', 'Password', 'required'); 
     if($this->form_validation->run()) 
     { 
      //true 
      $username = $this->input->post('username'); 
      $password = $this->input->post('password'); 
      //model function 
      $this->load->model('main_model'); 
      if($this->main_model->can_login($username, $password)) 
      { 
       $session_data = array( 
         'username'  =>  $username 
       ); 
       $this->session->set_userdata($session_data); 
       redirect('login/enter'); 
      } 
      else 
      { 
       $this->session->set_flashdata('error', 'Invalid Username or Password'); 
       redirect('login/index'); 
      } 
     } 
     else 
     { 
      //false 
      $this->index(); 
     } 
    } 
public function enter(){ 
     if($this->session->userdata('username') != '') 
     { 
      $this->load->view('includes/dashlink'); 
      $this->load->view('includes/dashheader'); 
      $this->load->view('index'); 
      $this->load->view('includes/dashfooter'); 


     } 
     else 
     { 
      redirect(base_url() . 'login/index'); 
     } 
    } 
public function logout() { 

$this->session->sess_destroy(); 
$data['message_display'] = 'Successfully Logout'; 
$this->load->view('login', $data); 

    } 

其他控制器:

function __construct() { 
      parent::__construct(); 
$this->load->library('session'); 
    if(!$this->session->userdata(' $session_data')){ 
    redirect(login); 
    } 
     } 
+0

嘗試改變'如果($這個 - >會話級>用戶數據( '$ session_data是')!){'到'if(!$ this-> session-> userdata('username')){' – Tpojka

+0

謝謝Tpojka – shahir

回答

0

在創建你可以按照以下起名會話:

$this->session->set_userdata('session_name',$session_data); //give session name 

檢查會話:

function __construct() { 
     parent::__construct(); 
     $this->load->library('session'); 
     if(!$this->session->userdata('session_name')){ 
      redirect(login); 
     } 
} 

,你也可以訪問會話數據:

$data_session = $this->session->userdata('session_name'); 
$username = $data_session['username]; 
+0

謝謝各位朋友。其工作 – shahir

+0

請投票並標記爲未來參考的權利。 – Shihas

相關問題