2015-10-15 82 views
2

我創建的登錄會話,但它不保存我存儲會話中的變量也不是跨越到其他頁面攜帶他們:CodeIgniter的會話數據不工作

控制器代碼:

function CheckDatabase($password) //This function is only run when password validation is correct.// 
{ 
    $username = $this->input->post('Username'); //Sets the username as a $username.// 
    $result = $this->User_model->Login($username, $password); 

    if($result) 
    { 
     $sess_array = array(); 
     foreach($result as $row) 
     { 
      $sess_array = array(//Makes an array of the data to be stored in the session.// 
      'UserID' => $row->UserID, 
      'Username' => $row->Username 
      ); 

      $this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.// 
     } 

     return TRUE; 
    } 

    else //Ran if the username or password aren't matched in the CIUsers database. Returns error message.// 
    { 
     $this->form_validation->set_message('CheckDatabase', 'Invalid login details.'); 
     return false; 
    } 
} 
同一個控制器上

指數函數(應該停止用戶回到登錄屏幕,但沒有)在主頁視圖

function index() //Default function that is run when this controller is called.// 
{ 

    if($this->session->userdata('logged_in')) 
    { 
     redirect('Home_controller'); 
    } 

    else 
    { 
     $this->load->helper(array('form')); 
     $this->load->view('Login_view'); 
    } 

} 

代碼(該瀏覽器被定向到一次登錄)

<?php echo $UserID .": ".$Username; ?> 

我得到這個錯誤顯示的會話數據:

消息:未定義的變量:用戶名

文件名:觀點/ Home_view.php

行號: 9

回答

2

要檢索會話變量,您在使用前做這樣的

$username = $this->session->userdata['logged_in']['Username']; 
echo $username; 
1

你必須從這樣的會議上檢索:

$user = $this->session->userdata('logged_in'); 
$Username = $user['Username']; 
$UserID = $user['UserID']; 
0

嘗試這樣

$newdata = array(
        'user_id' => $rows->id, 
        'user_name' => $rows->name, 
        'user_image' => $rows->user_image, 
        'user_email' => $rows->email, 
        'logged_in' => TRUE, 
        'user_type' => $rows->user_type 
       ); 
      } 
      $this->session->set_userdata($newdata); 

而在你的控制器檢查構建用戶會話

<?php 
    if (!defined('BASEPATH')) 
     exit('No direct script access allowed'); 
    class User extends CI_Controller { 
      public function __construct() { 
      parent::__construct(); 

     //Here we will check for the session value if it is true then the function will run else it will redirect to login page 

      if ($this->session->userdata('logged_in') == FALSE) { 
       redirect(site_url('your login page')); 
      } else { 
       // if there is session value 
       redirect(site_url('User/your function name'); 
      } 
    } 
} 
    ?> 

有關完整的交代閱讀本教程

http://w3code.in/2015/10/session-handling-in-codeigniter/

0
if($result) 
{ 
    $sess_array = array(); 
    foreach($result as $row) 
    { 
     $sess_array = array(//Makes an array of the data to be stored in the session.// 
     'UserID' => $row[0]->UserID, 
     'Username' => $row[0]->Username 
     ); 

     $this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.// 
    } 

    return TRUE; 

}