CodeIgniter browser back button showing history fix
Tagged:

You may have noticed an issue while coding on CodeIgniter that even though you have destroyed the session values (invoking the $this->session->sess_destroy()) after logging out the page, the browser back button still takes you to your previous history page. Even though we may have made a provision to check empty session parameters and log out the page if the session has been destroyed, the browser back button still displays the previous pages that we have browsed.

The session check if we have used, tends to be working while refreshing the page, and doesn't let us browse new pages further. However, the back button lets us browse previous history items and it may raise an issue on privacy. User's private data may be visible to others too.

This occurs because the browser back button goes to the history when clicked.
We can prevent it by setting a header with no-cache. To prevent it, we should put the code given below in our controller's constructor.


$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, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");