0

我正在使用 CodeIgniter 框架,并且在从一个控制器重定向到另一个控制器时无法维护会话数据。这是我设置会话数据的第一个控制器

//Ajax login function 
function ajax_login() {
$response = array();

//Recieving post input of email, password from ajax request
$email = $_POST["email"];
$password = $_POST["password"];
$response['submitted_data'] = $_POST;

//Validating login
$login_status = $this->validate_login($email, $password);
$response['login_status'] = $login_status;
if ($login_status == 'success') {

redirect(base_url() . 'index.php?admin/dashboard', 'refresh'); 
}


//Replying ajax request with validation response
echo json_encode($response);
}

//Validating login from ajax request
function validate_login($email = '', $password = '') {
$credential = array('email' => $email, 'password' => $password);

// Checking login credential for admin
$query = $this->db->get_where('admin', $credential);
if ($query->num_rows() > 0) {
$row = $query->row();
$this->session->set_userdata('admin_login', '1');
$this->session->set_userdata('admin_id', $row->admin_id);
$this->session->set_userdata('login_user_id', $row->admin_id);
$this->session->set_userdata('name', $row->name);
$this->session->set_userdata('login_type', 'admin');   
return 'success';
}

所以当我 vardump($this->session->userdata()) 我得到上面存储的所有数据并且重定向的控制器是

function dashboard()
{
if ($this->session->userdata('admin_login') != 1)
redirect(base_url(), 'refresh');
$page_data['page_name'] = 'dashboard';
$page_data['page_title'] = get_phrase('admin_dashboard');
$this->load->view('backend/index', $page_data);
}

at second controller I m getting an empty user data array

注意:我已经在两个控制器的 _construct() 中加载了会话库,即

function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');

/*cache control*/
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');

}

所以当我 vardump($this->session->userdata()) 我得到一个 NULL

4

1 回答 1

0

从 autoload.php 文件加载数据库和会话库

$autoload['libraries'] = array('database', 'session');

那么你就不需要在每个构造函数中添加这两个了。它将为应用程序生成一个独特的会话。试试看。

于 2018-08-20T07:13:23.253 回答