Part of the EllisLab Network
   
 
Session Timeout on Browser Close [Solved with patch]
Posted: 23 July 2007 03:04 PM   [ Ignore ]  
Summer Student
Total Posts:  24
Joined  04-03-2006

Hey Rick, after some research I found that the when you set the php’s session life time to 0, it’s dies after the browser is closed. (php site http://ar2.php.net/manual/en/function.setcookie.php). After debuged my cookie I’ve founded that was setted to 0 on the client also.
So we could have the same in codeigniter, it’s a small change.

At Session.php

<?php
//At method
function sess_run() {
...
...
//next is line 101
if (is_numeric($expiration))
{
if ($expiration > 0)
{
  $this
->sess_length = $this->CI->config->item('sess_expiration');
}
else if ($expiration == 0)
{
  $this
->sess_length = 0
}
else if ($expiration == -1)
{
  $this
->sess_length = (60*60*24*365*2);
}
}
...
...
}


//And at method
function sess_write() {
...
...
// At line 275
setcookie(
$this->sess_cookie,
$cookie_data,
(
$this->sess_length == 0) ? 0 : $this->sess_length + time(),
$this->CI->config->item('cookie_path'),
$this->CI->config->item('cookie_domain'),
0
);
...
...
}
?>

After this changes:
- sess_expiration = 0 it will timeout after browser closes
- sess_expiration = -1 it will receive the default 2 years value.

Hope you can add this.

Profile
 
 
Posted: 05 January 2009 10:13 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  6
Joined  12-17-2008

Though is only a small distraction, I would like to remind you to put the ; in the line:

$this->sess_length = 0

becoming

$this->sess_length = 0;

Hugz :D

Profile
 
 
Posted: 06 January 2009 07:07 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  6
Joined  12-17-2008

BTW,

I have made that change, and I was losing the session all the time…

So I’ve been checking Session class and saw one validation that was… hum… how do I say… incomplete after the fix you posted here…

This was the validation:

if (($session['last_activity'] + $this->sess_length) < $this->now)

I changed it to:

if (($this->sess_length!=0) && (($session['last_activity'] + $this->sess_length) < $this->now))

The reason was pretty simple… If you add 0 to the last_activity, off course it will be smaller than the $this->now value smile

So, I defined that it should only do that check if sess_length is different of 0 smile (based on the top fix/patch).

Hugz

Profile
 
 
Posted: 22 January 2009 10:54 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  379
Joined  10-08-2008

For those who don’t want to edit the actual Session class, here is a MY_Session that you can add to application/library and get the session timeout along side the normal nnn-minute timeout.

It works by adding this to your config:

$config['sess_browser'] = TRUE;

(I copied this in and hacked out some other custom code from my class, I apologize if it doesn’t work out of the box.)

File Attachments
MY_Session.php.zip  (File Size: 2KB - Downloads: 79)
 Signature 

Phil DeJarnett
OverZealous Creations, LLC
ZestyJobs - Powered by CI/DMZ


DataMapper OverZealous Edition: Extensions to DataMapper to improve its usability. Discuss

Profile
 
 
Posted: 25 February 2009 12:49 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  90
Joined  07-06-2006
OverZealous.com - 22 January 2009 10:54 PM

For those who don’t want to edit the actual Session class, here is a MY_Session that you can add to application/library and get the session timeout along side the normal nnn-minute timeout.

It works by adding this to your config:

$config['sess_browser'] = TRUE;

(I copied this in and hacked out some other custom code from my class, I apologize if it doesn’t work out of the box.)


Sorry, but I don’t understand how can I make this work to have the session destroyed when I close the browser…

can you explain a bit more?

 Signature 

Guido Rossi

Profile
 
 
Posted: 25 February 2009 12:53 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  379
Joined  10-08-2008

First, place the included MY_Session.php file in application/libraries

This will override some of the functionality built into the CodeIgniter Session class.

Second, add the config line posted above to your config.

Now the Session Library will set the cookie using a timeout of ‘0’, which tells the browser to delete the cookie as soon as the browser as closed.

It doesn’t destroy the session, but it prevents the session from persisting after the browser is closed or opened.  The session itself will be destroyed eventually, usually after the normal inactive timeout passes.

 Signature 

Phil DeJarnett
OverZealous Creations, LLC
ZestyJobs - Powered by CI/DMZ


DataMapper OverZealous Edition: Extensions to DataMapper to improve its usability. Discuss

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77577 Total Logged-in Users: 22
Total Topics: 101563 Total Anonymous Users: 3
Total Replies: 544413 Total Guests: 195
Total Posts: 645976    
Members ( View Memberlist )