1

我正在使用 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');
}
4

1 回答 1

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');

或者

        $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');
    }
}

这样页面缓存就会被移除

于 2012-11-08T04:51:52.727 回答