Codeigniter pressing logout button and disable the back browser button
I tired to implement this option but it doesn't works well. So i implement new logic on this.
Simply check is session is set in every main methods. Below code help you
In logout(define in controller)
function __construct()
{
parent::__construct();
ob_start(); # add this
}
public function logout()
{
$this->load->driver('cache');
$this->session->sess_destroy();
$this->cache->clean();
ob_clean();
redirect('home'); # Login form or some other page
}
In dashboard(Function)
public function home()
{
$logged_in = $this->session->userdata('logged_in');
if($logged_in != TRUE || empty($logged_in))
{
#user not logged in
$this->session->set_flashdata('error', 'Session has Expired');
redirect('user_logging'); # Login view
}
else
{
#user Logged in
$this->load->view("viewname",$data);
}
}
In Login(function)
$session = array(
'username' => $name,
'logged_in' => TRUE
);
$this->session->set_userdata($session);
CodeIgniter back button after logout
Add this to prevent caching of the previous page:
header("cache-Control: no-store, no-cache, must-revalidate");
header("cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
View the Dashboard after logout with browser back button
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");
}
CodeIgniter session issue after sign out browser back button landed to the secured page
Include these headers in the constructor function of the controller to prevent the caching of previous page
If you want the Code Igniter's way of doing it include the below code
$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');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
$this->output->set_header('Pragma: no-cache');
PHP's way of doing it use the below lines of code
header("cache-Control: no-store, no-cache, must-revalidate");
header("cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
Avoid going to homepage when clicked Back button after logout
Hopefully you're using templates for your views and this will be painless but make sure this is in the head of each page you don't want accessible by the back button.
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE, NO-STORE, must-revalidate">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT=0>
Just to explain what is happening. The page is loading from your browser cache meaning the browser thinks your user is still logged in. The above lines make the browser revalidate the page on every load and won't load it from it's own cache.
Related Topics
Storage in Laravel Says Symlink - No Such File
How to Validate on Max File Size in Laravel
MySQL Transactions Within Transactions
Check If a PHP Cookie Exists and If Not Set Its Value
How to Convert the Time from Am/Pm to 24 Hour Format in PHP
Phpexcel_Writer_Exception with Message "Could Not Close Zip File PHP://Output."
Getting Data Posted in Between Two Dates
Type Hinting in PHP 7 - Array of Objects
How Is Annotation Useful in PHP
Advantages/Inconveniences of Heredoc VS Nowdoc in PHP
Fatal Error - Too Many Open Files
How to Display Woocommerce Category Image
Windows 7 PHP + Symfony2 Terribly Slow
Why Are Certain Types of Prepared Queries Using Pdo in PHP with MySQL Slow
Zip All Files in Directory and Download Generated .Zip
Easy Login Script Without Database
Mssql Query Issue in PHP and Querying Text Data
How to Post Multiple <Input Type="Checkbox" /> as Array in PHP