如何在codeigniter中关闭浏览器时销毁会话 [英] How to destroy session with browser closing in codeigniter

查看:38
本文介绍了如何在codeigniter中关闭浏览器时销毁会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我用 codeigniter 开发了一个 web 应用程序.我在那里遇到了与会话相关的严重问题.

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

问题场景:

如果用户 A 登录到应用程序,则用户 ID 在会话中设置.完成任务后,用户 A 关闭浏览器并离开计算机.过了一会儿,用户 B 来打开浏览器,看到应用程序处于登录状态.或者当用户 B 写下 url 并按 Enter 时,它会直接重定向到应用程序中,而无需使用前一个会话进行任何身份验证.

If user A logged into the application then the user id set in session. After doing task user A closed his browser and leave the computer. A little while later user B came and open browser and see the application was in logged in state. or when user B write down the url and press enter it directly redirected into the application without any authentication by using the previous session.

我为会话使用了以下配置:

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 和异步请求.关闭窗口时,调用 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
         }
      }
   }  

这篇关于如何在codeigniter中关闭浏览器时销毁会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆