我第一次面临会话销毁的新问题。在今天之前,我会点击注销使用这个:
function logout()
{
$this->session->unset_userdata("logged_in");
$this->session->sess_destroy();
redirect(base_url(), 'refresh');
}
那届会议很容易就被破坏了,但今天它没有发挥作用。这是我开始会话的代码。
function checkDataBase()
{
$this->load->library('session');
$email = $this->input->post('email');
$password = $this->input->post('password');
$result = $this->login_model->contractor_login($email, $password);
// die;
// echo "<pre>";print_r($result);die;
if ($result) {
if (isset($_POST['remember'])) {
$this->input->set_cookie('email', $_POST['email'], 3600);
$this->input->set_cookie('password', $_POST['password'], 3600);
}
foreach ($result as $row);
$role_id = 2;
$session_data = array(
'id' => $row->id,
'name' => $row->name,
'country_id' =>$row->country_id,
'category_id' =>$row->category_id,
'company_name' =>$row->company_name,
'email' =>$row->email,
'address1' =>$row->address1,
'counties' =>$row->counties,
'phone_number' =>$row->phone_number,
//'business_status' =>$business_status,
'role_id' => $role_id
);
$this->session->set_userdata('logged_in', $session_data);
return TRUE;
} else {
$this->session->set_flashdata('message', 'Wrong Username or Password');
redirect("contractor/login");
}
}
从Codeigniter会话类文档中,关于Flashdata,我们可以阅读:
CodeIgniter支持"flashdata",即仅可用于下一个服务器请求的会话数据,然后自动清除。你的问题可能是,当你重定向时,这个过程需要多个请求,从而清除你的flash数据。
要查看情况是否如此,只需将以下代码添加到重定向到的控制器的构造函数中:
$this->session->keep_flashdata('message');
这将为另一个服务器请求保留flash数据,以便以后使用。
我得到了会话销毁的解决方案。事实上,问题是我的浏览器创建了缓存,这就是为什么我认为会话没有被破坏。当我进行研究时,我得到了问题的解决方案。
这里是我找到解决方案的链接。
http://www.robertmullaney.com/2011/08/13/disable-browser-cache-easily-with-codeigniter/I
我在"应用程序/库"中创建了一个新文件
使用此代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Output extends CI_Output {
function nocache()
{
$this->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->set_header('Pragma: no-cache');
}
}
/* End of File */
然后我调用了nocache();将函数转换为控制器的构造函数
$this->output->nocache();
问题已经解决了。