我正在使用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
答案 0 :(得分:0)
从autoload.php文件加载数据库和会话库
$autoload['libraries'] = array('database', 'session');
然后,您无需在每个构造函数中都添加这两个选项。它将为应用程序生成一个唯一的会话。试试看。