I’m currently learning the basics of MVC using codeigniter as my framework and I created a login page. Now I already redirected a user after he logged in and stored a session data but the problem is after I log in (where in I redirected into another page) then pressed the back button the login page still displays unless I highlight the URL in the address bar and pressed enter wherein I go back again to the redirected page. Well I’m aware that this is because of the session check I’ve made but I need a solution on how I can solve it for the user to not go back again to login page unless he logged out. Same thing for logged out user not to go back to a certain page unless he/she logs in again.
To give a more details what exactly I wanted to happen is just like in facebook login wherein after you logged in and clicked the back button on browser the facebook will still redirect you to your homepage and not on the login page. samething goes to logout.
Well here’s my controller so far I think this explains it all. Please do correct me if I made some redundancies in my code and understand that I’m a newbie on this. 🙂
public function index()
{
$data['error'] = null;
$username = $this->input->post('username');
$password = $this->input->post('password');
$data['login'] = $this->login_model->get_login_credentials($username,$password);
$data['session'] = $this->login_model->get_session($username);
//CHECK IF THE $PAGE VIEW IS EXISTING
if ( ! file_exists('application/views/pages/login.php'))
{
// IF PAGE DOESN'T EXISTS SHOW 404 ERROR
show_404();
}
if(($this->input->post('login')=='Login'&&$data['login']==1)||($username==$data['session']['username'])){
//SUCCESS LOGIN
$this->load->helper('url');
redirect('news');
}
if($this->input->post('login')=='Login'&&$data['login']!=1){
//LOGIN FAILED
$data['error'] = "Invalid login credentials.";
}
//FOR THE TITLE PART
$data['title'] = ucfirst('login');
$this->load->view('pages/login/login', $data);
$this->load->view('templates/footer', $data);
}
This question is already solved here: Clear cache on back press to prevent going back on login page or previous page after logout by Hashem Qolami Same question but in different perspective.