问题
Is there somewhere in codeigniter where I could configure an automatic redirect to my root page if the user's session is expired (I am not trying to extend the session expiration's time)?
Situational example:
User logs in, leaves page idle for one day, returns to page that requires validated session and sees PHP errors everywhere.
I'd prefer for anything in this situation to be redirected to my root login page.
Thank you!
回答1:
put this content into your constructor function because when the controller run constructor will execute first.
function __construct() {
parent::__construct();
if(empty($this->session->userdata("login_session_user")))
{
redirect(site_url(),'refresh');
}
}
回答2:
Use below code in each function and place your function code inside the if loop. This will be use full for you. Thank you .
$this->load->helper('url');
if($this->session->userdata('logged_in'))
{
// You function code should be placed here .
}
else{
redirect(site_url(),'refresh');
}
来源:https://stackoverflow.com/questions/30623714/if-session-expires-automatically-redirect-home-codeigniter