how to disable the back button in codeigniter?

2019-09-16 19:08发布

问题:

I am newbie in Codeigniter PHP. I have developed a simple user management system in this on user login i have set database session. I also destroy them on logout. My Caching is also turned off. Even though when i click on back button on browser i can see my dashboard,if i click on any link then it redirects me to login,but it shows me the dashboard at first which is a bug. Please help me solve it.

Model:

public function login ()
{
    $user = $this->get_by(array(
        'email' => $this->input->post('email'),
        'password' => $this->hash($this->input->post('password')),

    ), TRUE);

    if (count($user)) {
        // Log in user
        $data = array(
            'name' => $user->name,
            'email' => $user->email,


            'id' => $user->id,

            'loggedin' => TRUE,
        );
        $this->session->set_userdata($data);
    }
}

public function logout ()
{
    $this->session->sess_destroy();
    $this->session->unset_userdata('logged_in','id','name','email');

}

public function loggedin ()
{
    return (bool) $this->session->userdata('loggedin');
}

Controller:

public function login ()
{

    // Redirect a user if he's already logged in
    $dashboard = 'user/dashboard';
    $this->userlogin_m->loggedin() == FALSE || redirect($dashboard);

    // Set form
    $rules = $this->userlogin_m->rules;
    $this->form_validation->set_rules($rules);

    // Process form
    if ($this->form_validation->run() == TRUE) {
        // We can login and redirect
        if ($this->userlogin_m->login() == TRUE) {
            redirect($dashboard);
        }
        else {
            $this->session->set_flashdata('error', 'That email/password combination does not exist');
            redirect('user/secure/login', 'refresh');
        }
    }

    // Load view
    $this->data['subview'] = 'user/secure/login';
    $this->load->view('user/_layout_modal', $this->data);
}

public function logout ()
{
    $this->userlogin_m->logout();
    redirect('user/secure/login');
}

回答1:

Try to disable cache by using :

<META HTTP-EQUIV="Pragma" CONTENT="private">
<META HTTP-EQUIV="Cache-Control" CONTENT="private, max-age=5400, pre-check=5400">
<META HTTP-EQUIV="Expires" CONTENT="<?php echo date(DATE_RFC822,strtotime("1 day")); ?>">

Also try to add below :

$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");