我的CodeIgniter Session()类有问题,而当用户注销时,我会收到错误。问题是,我在页面的页眉上有一个用户HTML块,显示他的头像和信息。问题是当我在我的注销控制器中这样做时:
if ($this->session->userdata('is_logged_in') == 1) {
$data['logged_in'] = TRUE;
$this->session->sess_destroy();
}
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
然后在我的HTML视图中,我问:
<?php
if ($this->session->userdata('is_logged_in') == 1) {
// Output HTML
}
?>
但是它为它所缺少的所有会话变量生成错误。
所以我的问题;为什么"is_loged_in"在检查时仍然存在,而其余数据却不见了。
CI 2.1.0中的一个错误是,如果调用$this->session->sess_destroy()
,它不会取消设置用户数据。
可以在此处找到解决该问题的拉取请求:https://github.com/EllisLab/CodeIgniter/pull/1323.
修复非常琐碎。您必须更改文件system'libraries'Session.php
。您必须在函数sess_destroy()
的末尾添加以下行:
// Kill session data
$this->userdata = array();
所以sess_destroy()
看起来是这样的:
/**
* Destroy the current session
*
* @return void
*/
public function sess_destroy()
{
// Kill the session DB row
if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
{
$this->CI->db->where('session_id', $this->userdata['session_id']);
$this->CI->db->delete($this->sess_table_name);
}
// Kill the cookie
setcookie(
$this->sess_cookie_name,
addslashes(serialize(array())),
($this->now - 31500000),
$this->cookie_path,
$this->cookie_domain,
0
);
// Kill session data
$this->userdata = array();
}