If session expires, automatically redirect home - Codeigniter

那年仲夏 提交于 2020-01-15 12:09:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!