Welcome Guest, Not a member yet? Register   Sign In
How to clear CI session cookie as soon as the browser is closed?
#1

[eluser]Unknown[/eluser]
Hi everyone!

I have a problem with the session in CI. I made a login system. When the user logged in successfully, I would write the data to the session. And what I want is that when the user closes browser, the session data must be cleared immediately as well. But the problem is that when I open the browser again, the user is still logged in. It seems CI stores the session data in cookie (not in session file).

I hope that some of you can help.
Thanks in advance.
#2

[eluser]Phil Sturgeon[/eluser]
You need to set the number of seconds before session expiry in config.php as CI stores sessions in a cookie. That means they can last for a very long time by default (0 = unlimited) so set 3600 to log them out 1 hour of inactivty for example.

If this is not good enough, you could use one of the third-party session libraries that use native PHP sessions (which will expire when you close the browser).

The CI team have good reason for using cookies over sessions (server inconsistencies being and lack of control to name a few) but if you NEED to use normal sessions, there is plenty of help to do so on the wiki and around the forums.
#3

[eluser]Unknown[/eluser]
Hello pyromaniac,

Thanks for your reply. I will give it a try.

Cheers!
#4

[eluser]moodh[/eluser]
If you got access to the php config you can set the cookie lifetime to 0 which means they destroy when the browser closes. I havn't had any success with this (yet) so I'll leave the testing up to you =P
#5

[eluser]CI_NYC[/eluser]
After a couple of days of digging, I found the problem is in the libraries/Session.php. when you set session length to 0, it actually set it to 0+time().

If you would like to have that fixed, change the line in _set_cookie() that looks like:
$this->sess_expiration + time(),

to
$this->sess_length + ($this->sess_length == 0) ? 0 : time(),


In the same file, setting session length to 0 actually is be converted to 2 years.
if ($this->sess_expiration == 0)
{
$this->sess_expiration = (60*60*24*365*2);
}

You need to comment out that too.




Theme © iAndrew 2016 - Forum software by © MyBB