我正在使用php codeigniter创建一个网站。当我登录时,它会创建一个会话并重定向到主页。当我退出时,它会破坏会话。当我写主页的网址时,它工作正常。但如果我点击浏览器上的“返回一页按钮”,它将返回主页。即使主页应该检查我是否登录。我的代码如下。我希望我能清楚地解释我的困境。如果没有,那么请问。提前谢谢。
public function signin()
{
$this->form_validation->set_rules('email2', 'E-mail', 'required|valid_email');
$this->form_validation->set_rules('password2', 'Password', 'required|min_length[5]');
$query = "select password from user where email = '{$this->input->post('email2')}';";
if($this->form_validation->run())
{
if($this->doj_database->check_signin($query, $this->input->post('password2')))
{
$query = "select * from user where email = '{$this->input->post('email2')}';";
$data['user'] = $this->doj_database->search_with_email($query);
$newdata = array('logged_in' => TRUE);
$this->session->set_userdata($newdata);
$url = "/DIRTY_ONLINE_JUDGE/goto_home/{$data['user']['user_id']}";
redirect($url);
}
else
{
$this->load->view('wrong_login');
}
}
else
{
$this->load->view('wrong_login');
}
}
public function goto_home($id)
{
if($this->session->userdata('logged_in'))
{
$data['user'] = $this->doj_database->search_with_id($id);
$data['user']['password'] = $this->encrypt->decode($data['user']['password']);
$this->load->view('home', $data);
}
else
{
$this->load->view('not_logged_in');
}
}
public function logout()
{
$this->session->sess_destroy();
$this->load->view('index');
}
答案 0 :(得分:3)
添加
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0');
这在
public function goto_home($id)
{
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0');
OR
$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
$this->output->set_header('Pragma: no-cache');
if($this->session->userdata('logged_in'))
{
$data['user'] = $this->doj_database->search_with_id($id);
$data['user']['password'] = $this->encrypt->decode($data['user']['password']);
$this->load->view('home', $data);
}
else
{
$this->load->view('not_logged_in');
}
}
这样就可以删除页面缓存