0

I m new to codeigniter framework. I know this question has been asked many times. I have done R & D but i m unable to get it working. I m stuck at the moment. I m using the codeigniter framework 2.2.2. I have used the session for login as following:

Login Controller

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

When the user is logout i m calling the logout function in user controller:

User Controller Logout function:

function logout() {

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

Up to this everything is working fine. I can logout to login page but when i click on the browser back button i can see the dashboard. This shouldnt be done. My User controller constructor is checking the session or 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;
    }
}

I have done everything i can but its not working. I have followed this Codeigniter pressing logout button and disable the back browser button. But it couldn't solve my problem. Any help in this regard would be greatly appreciated. Thanks in advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
waheed shah
  • 494
  • 7
  • 19

2 Answers2

1

You can right a function to clear cache and call it in the constructor.

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");
}
0

You must to put cache-control headers, like this:

$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");
Evgeny Ruban
  • 1,357
  • 1
  • 14
  • 20
  • I was doing this before but it wasnt working as i didnt know where to put it exact. Thanks for your comment @EugeneR – waheed shah Mar 08 '18 at 05:55