使用浏览器后退按钮注销后查看仪表板

时间:2018-03-06 14:12:42

标签: php codeigniter login logout

我是codeigniter框架的新手。我知道很多次都会问过这个问题。我做过R& D但我无法让它工作。我现在卡住了。我正在使用codeigniter框架2.2.2。我使用会话登录如下:

登录控制器

public function loginMe()
{
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required|max_length[32]|');

    if($this->form_validation->run() == FALSE)
    {
        $this->index();
    }
    else
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $result = $this->login_model->loginMe($username, $password);

        if(count($result) > 0)
        {
            foreach ($result as $res)
            {
                $sessionArray = array('userId'=>$res->userId,
                                        'role'=>$res->roleId,
                                        'roleText'=>$res->role,
                                        'name'=>$res->name,
                                        'isLoggedIn' => TRUE
                                );

                $this->session->set_userdata($sessionArray);

                redirect('/dashboard');
            }
        }
        else
        {
            $this->session->set_flashdata('error', 'Username or password mismatch');

            redirect('/login');
        }
    }
}

当用户注销时,我在用户控制器中调用注销功能:

用户控制器注销功能:

function logout() {

    $this->session->sess_destroy ();
     ob_clean();
    redirect ('login');
}

到目前为止一切正常。我可以退出登录页面但是当我点击浏览器后退按钮时,我可以看到仪表板。这不应该做。 我的用户控制器构造函数正在检查会话或isloggedin()。

public function __construct()
{
    parent::__construct();
    ob_start();
    $this->load->model('user_model');

    $this->isLoggedIn();   
}
function isLoggedIn() {
    $isLoggedIn = $this->session->userdata ( 'isLoggedIn' );

if (! isset ( $isLoggedIn ) || $isLoggedIn != TRUE || empty($isLoggedIn)) {
    $this->session->set_flashdata('error', 'Session has Expired');
        redirect ( 'login' );
    } else {
        $this->role = $this->session->userdata ( 'role' );
        $this->vendorId = $this->session->userdata ( 'userId' );
        $this->name = $this->session->userdata ( 'name' );
        $this->roleText = $this->session->userdata ( 'roleText' );
        $this->lastLogin = $this->session->userdata ( 'lastLogin' );

        $this->global ['name'] = $this->name;
        $this->global ['role'] = $this->role;
        $this->global ['role_text'] = $this->roleText;
        $this->global ['last_login'] = $this->lastLogin;
    }
}

我已尽我所能,但它无法正常工作。我遵循了这个Codeigniter pressing logout button and disable the back browser button。但它无法解决我的问题。在这方面的任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用函数清除缓存并在构造函数中调用它。

function clear_cache()
{
   $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");
}

答案 1 :(得分:0)

您必须放置缓存控制标头,如下所示:

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