How to destroy session with browser closing in codeigniter

前端 未结 9 1183
陌清茗
陌清茗 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:13

    Simply set sess_expiration =0 in application/config/config.php file as stated in Codeigniter documentation

    0 讨论(0)
  • 2020-12-06 07:16

    Well, I don't know if you already had the solution, but I found myself in the same scenario in CodeIgniter version 3.0. In config.php, go to Session Variables section and you can set:

    $config['sess_expiration'] = 0;
    

    sess_expiration: The number of seconds you would like the session to last. If you would like a non-expiring session (until browser is closed) set the value to zero: 0

    0 讨论(0)
  • 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
             }
          }
       }  
    
    0 讨论(0)
  • 2020-12-06 07:22

    for CI-3.0.4 locate the items below inside "config.php" and set accordingly

    $config['sess_expiration'] = -1;
    $config['sess_time_to_update'] = -1;
    
    0 讨论(0)
  • 2020-12-06 07:23

    Add a logout button.

    Have that button destroy the session with CI's built in destroy function.

    $this->session->sess_destroy();
    
    0 讨论(0)
  • 2020-12-06 07:25

    Edit the config.php file and set sess_expire_on_close to true.

    e.g

    $config['sess_expire_on_close'] = TRUE;
    
    0 讨论(0)
提交回复
热议问题