php 如何在codeigniter中关闭浏览器时销毁会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13174297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to destroy session with browser closing in codeigniter
提问by Ariful Islam
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 写下 url 并按下回车键时,它会使用前一个会话直接重定向到应用程序中,无需任何身份验证。
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 中的浏览器或浏览器选项卡来销毁所有会话?
采纳答案by artragis
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
}
}
}
回答by mohsin139
Edit the config.phpfile and set sess_expire_on_closeto true.
编辑config.php文件并设置sess_expire_on_close为true.
e.g
例如
$config['sess_expire_on_close'] = TRUE;
回答by colonelclick
Add a logout button.
添加注销按钮。
Have that button destroy the session with CI's built in destroy function.
让该按钮使用 CI 的内置销毁函数销毁会话。
$this->session->sess_destroy();
回答by viperfx
Have a look at this, and see if this helps.
看看这个,看看这是否有帮助。
回答by José Miguel
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 Variablessection and you can set:
好吧,我不知道您是否已经有了解决方案,但我发现自己在 CodeIgniter 3.0 版中遇到了相同的场景。在 中config.php,转到Session Variables部分,您可以设置:
$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
sess_expiration:您希望会话持续的秒数。如果您想要一个不过期的会话(直到浏览器关闭),请将值设置为零:0
回答by sandeep kumar
in $config.phpfile
在$config.php文件中
change $config['sess_expiration'] = 7200;
to
到
$config['sess_expiration'] = 0;
回答by Vipin Choubey
Simply set sess_expiration =0in application/config/config.phpfile
as stated in Codeigniter documentation
只需设置sess_expiration =0在application/config/config.php文件笨中陈述文档
回答by bungdito
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;
回答by jeffsque
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;

