How to destroy session with browser closing in codeigniter

前端 未结 9 1185
陌清茗
陌清茗 2020-12-06 06:55

Recently I have developed a web application with codeigniter. I am facing a session related problem there badly.

Problem scenario:

If user

9条回答
  •  北海茫月
    2020-12-06 07:19

    You can use javascript and asynchron request. When you close the window, the handler of window.onunload is called

    var unloadHandler = function(e){
            //here ajax request to close session
      };
    window.unload = unloadHandler;
    

    To solve the problem of redirection, php side you can use a counter

      class someController  extends Controller {
    
       public function methodWithRedirection(){
    
            $this->session->set_userdata('isRedirection', 1);
            //here you can redirect  
       }
    }
    class homeController extends Controller{
       public function closeConnection(){
            $this->load->library('session');
            if($this->session->userdata('isRedirection')!== 1){
                //destroy session
             }
          }
       }  
    

提交回复
热议问题