CodeIgniter Session 不会破坏
CodeIgniter Session won't destroy
我的登录功能可以正常工作,它有一个数组来显示会话数据,但是当我单击注销时,它会将我重定向到登录页面,但如果我手动加载会员页面,它不会清除会话数据.
控制器功能
public function logout(){
$this->session->sess_destroy();
redirect('site/login');
}
public function members(){
if ($this->session->userdata('is_logged_in')){
$data['title'] = "Members";
$this->load->view('view_header');
$this->load->view("view_members", $data);
}
else{
redirect('site/restricted');
}
}
型号
class Model_users extends CI_Model {
public function canLogin(){
$this->db->where('Username', $this->input->post('Username'));
$this->db->where('Password', md5($this->input->post('Password')));
$query = $this->db->get('user_registration');
if ($query->num_rows() == 1){
return true;
}
else{
return false;
}
}
}
会员视图
<?php
echo "<pre>";
print_r ($this->session->all_userdata());
echo "</pre>";
?>
<a href='<?php echo base_url()."index.php/site/logout"?>'>Logout</a>
不确定我在这里遗漏了什么,因为注销功能运行但似乎没有清除会话。
尝试在您的 Home 控制器中创建一个清理缓存的函数,如下所示:
function clear_cache()
{
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
}
所以从控制器的构造函数中调用它
class Home extends CI_Controller
{
function __construct()
{
parent:: __construct();
$this->clear_cache();
}
}
希望对您有所帮助。
我的登录功能可以正常工作,它有一个数组来显示会话数据,但是当我单击注销时,它会将我重定向到登录页面,但如果我手动加载会员页面,它不会清除会话数据.
控制器功能
public function logout(){
$this->session->sess_destroy();
redirect('site/login');
}
public function members(){
if ($this->session->userdata('is_logged_in')){
$data['title'] = "Members";
$this->load->view('view_header');
$this->load->view("view_members", $data);
}
else{
redirect('site/restricted');
}
}
型号
class Model_users extends CI_Model {
public function canLogin(){
$this->db->where('Username', $this->input->post('Username'));
$this->db->where('Password', md5($this->input->post('Password')));
$query = $this->db->get('user_registration');
if ($query->num_rows() == 1){
return true;
}
else{
return false;
}
}
}
会员视图
<?php
echo "<pre>";
print_r ($this->session->all_userdata());
echo "</pre>";
?>
<a href='<?php echo base_url()."index.php/site/logout"?>'>Logout</a>
不确定我在这里遗漏了什么,因为注销功能运行但似乎没有清除会话。
尝试在您的 Home 控制器中创建一个清理缓存的函数,如下所示:
function clear_cache()
{
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
}
所以从控制器的构造函数中调用它
class Home extends CI_Controller
{
function __construct()
{
parent:: __construct();
$this->clear_cache();
}
}
希望对您有所帮助。