如何销毁会话与浏览器关闭在codeigniter

问题描述:

最近我开发了一个带codeigniter的web应用程序。

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

我使用以下会话配置: / p>

I used the following configuration for session:

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 1800;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;



现在我的问题是如何通过关闭codeigniter中的浏览器或浏览器标签来销毁所有的会话?

Now my question is how can i destroy all the session with closing browser or browser tab in codeigniter?

您可以使用javascript和asynchron请求。
当关闭窗口时,调用window.onunload的处理程序

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;

为了解决重定向的问题,php端可以使用计数器

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
         }
      }
   }