2015-10-15 93 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; 

}