如何在codeigniter中使用浏览器关闭来破坏会话

时间:2022-11-23 20:44:32

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

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

Problem scenario:

问题场景:

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.

如果用户A登录到应用程序,则会话中设置用户ID。执行任务后,用户A关闭浏览器并离开计算机。不久之后,用户B来到并打开浏览器,看到应用程序处于登录状态。或者当用户B记下网址并按下输入时,直接将其重定向到应用程序,而无需使用上一个会话进行任何身份验证。

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;

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

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

8 个解决方案

#1


5  

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

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

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

要解决重定向问题,php方面你可以使用一个计数器

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

#2


6  

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

编辑config.php文件并将sess_expire_on_close设置为true。

e.g

例如

$config['sess_expire_on_close'] = TRUE;

#3


3  

Add a logout button.

添加注销按钮。

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

让那个按钮用CI的内置销毁功能破坏会话。

$this->session->sess_destroy();

#4


2  

Have a look at this, and see if this helps.

看看这个,看看这是否有帮助。

https://github.com/EllisLab/CodeIgniter/wiki/PK-Session

https://github.com/EllisLab/CodeIgniter/wiki/PK-Session

#5


0  

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

好吧,我不知道你是否已经有了解决方案,但我发现自己处于CodeIgniter 3.0版的同一场景中,在config.php中,转到Session Variables部分,你可以设置:

$config['sess_expiration'] = 0;

$ config ['sess_expiration'] = 0;

'sess_expiration'

'sess_expiration'

The number of SECONDS you want the session to last.
Setting to 0 (zero) means expire when the browser is closed.

#6


0  

Open the general config file application/config/config.php, search for $config[‘sess_expiration’]

打开常规配置文件application / config / config.php,搜索$ config ['sess_expiration']

change

更改

$config['sess_expiration'] = 7200;

to

$config['sess_expiration'] = -1;

#7


0  

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

对于CI-3.0.4,请在“config.php”中找到下面的项目并进行相应设置

$config['sess_expiration'] = -1;
$config['sess_time_to_update'] = -1;

#8


0  

in $config.php file

在$ config.php文件中

change  $config['sess_expiration'] = 7200;

to

$config['sess_expiration'] = 0;

#1


5  

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

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

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

要解决重定向问题,php方面你可以使用一个计数器

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

#2


6  

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

编辑config.php文件并将sess_expire_on_close设置为true。

e.g

例如

$config['sess_expire_on_close'] = TRUE;

#3


3  

Add a logout button.

添加注销按钮。

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

让那个按钮用CI的内置销毁功能破坏会话。

$this->session->sess_destroy();

#4


2  

Have a look at this, and see if this helps.

看看这个,看看这是否有帮助。

https://github.com/EllisLab/CodeIgniter/wiki/PK-Session

https://github.com/EllisLab/CodeIgniter/wiki/PK-Session

#5


0  

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

好吧,我不知道你是否已经有了解决方案,但我发现自己处于CodeIgniter 3.0版的同一场景中,在config.php中,转到Session Variables部分,你可以设置:

$config['sess_expiration'] = 0;

$ config ['sess_expiration'] = 0;

'sess_expiration'

'sess_expiration'

The number of SECONDS you want the session to last.
Setting to 0 (zero) means expire when the browser is closed.

#6


0  

Open the general config file application/config/config.php, search for $config[‘sess_expiration’]

打开常规配置文件application / config / config.php,搜索$ config ['sess_expiration']

change

更改

$config['sess_expiration'] = 7200;

to

$config['sess_expiration'] = -1;

#7


0  

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

对于CI-3.0.4,请在“config.php”中找到下面的项目并进行相应设置

$config['sess_expiration'] = -1;
$config['sess_time_to_update'] = -1;

#8


0  

in $config.php file

在$ config.php文件中

change  $config['sess_expiration'] = 7200;

to

$config['sess_expiration'] = 0;