2012-11-08 56 views
1

我正在用php codeigniter创建一个网站。当我登录时,它会创建一个会话并重定向到主页。当我注销时,它会破坏会话。当我编写主页的网址时,它工作正常。但是,如果我点击浏览器上的“返回一页按钮”,它会返回主页。即使主页应该检查我是否登录。我的代码如下。我希望我在解释我的困境时很清楚。如果没有,请问。提前致谢。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'); 
} 

回答

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

让页面缓存将被删除

+1

非常感谢。它正在工作:D –