tags:

views:

64

answers:

1

The session loads fine for the most part but it is randomly clearing all of the data some times and I don't know why:

Create my new session:

$session = CGI::Session->new( "driver:file",undef,{Directory => '/tmp'} );

$session->param('logged_in', 1);
$session->expire('logged_in', '+10m');

$session->expire('+1h');

Now when I go to another page and load the session I do:

$session = CGI::Session->load ( "driver:file", $sid, {Directory => '/tmp'} );

return 0 if $session->is_expired;

return 0 if !$session->param('logged_in');

return $session;

The problem I have is that sometimes, before the 10 minute mark is up the 'logged_in' param is empty when it should not be. Why could this be happening?

+1  A: 

First, you do not seem to be using strict: You should. Second, don't use indirect object notation. I.e., use CGI::Session->new.

To find out what is going on, use the sequential id generator for debugging and make sure you are looking at the session you think you are looking at. Make sure you create the session on log on, but from that point on, you load it.

Check how you are keeping track of the session id: Are you using cookies, query string parameters or from parameters? Make sure the correct session id is available at all times.

Sinan Ünür
I pass the correct SID through query string, the same thing is happening even if I use load. When using load, i check is_empty and it is randomly empty. Could my host have a time limit on how long files can exist in the /tmp directory?
hmmm seems to be working now that switched from indirect object notation... not sure why though, i'll keep an eye out fora little.
@user105033 If not using indirect object notation solved your problem for sure, then one of the gotchas listed in http://perldoc.perl.org/perlobj.html#Indirect-Object-Syntax is responsible. Possibly, `new` was being called as a subroutine and not a method.
Sinan Ünür