Hello Monks,
I am using CGI::Session for data persistence. I use a common subroutine, get_session(), to either create a new session or retrieve an existing one (which has not expired). In 'login.cgi', get_session() will return a new session and in the subsequent scripts, (for example, form.cgi), it will return the existing session. This is working fine and I have no issues.
-------------------------------------------- login.cgi: -------------------------------------------- #!/usr/bin/perl -w use strict; use warnings; use lib "/path/to/lib/"; use CGI; use CGI::Session; # new query object my $cgi = CGI->new; # Get a new session my $session = get_session($cgi); print $session->header(); ... HTML template commands... ... print $template->output(); -------------------------------------------- form.cgi: -------------------------------------------- #!/usr/bin/perl -w use strict; use warnings; use lib "/path/to/lib/"; use CGI; use CGI::Session; # new query object my $cgi = CGI->new; # Get a new session my $session = get_session($cgi); print $session->header(); ... HTML template commands... ... print $template->output(); --------------------------------------------------------- get_session.pm (to create or retrieve a session ) --------------------------------------------------------- sub get_session { my $cgi = shift; my ($session, $exist_sess_id); # Query the cookie and retrieve existing session id, if exists $exist_sess_id = $query->cookie( "CGISESSID" ) or undef; # Retrieve or Create a session $session = CGI::Session->new( undef, $exist_sess_id, { Directory => '/usr/sessions' }) or die "can't create session: $!"; # Set expiration time $session->expire( "+10m" ); return $session; }
But, note that in the above code, if I close a session (let us say by killing the browser window), the session will be active for 10 minutes and if I call login.cgi before it expires, I still get the old session, which I wanted to eliminate. As per the new requirement, I should always get a new session in 'login.cgi'.
Hence, I created another sub,'delete_existing_session()', and called that before get_session() in 'login.cgi'. The idea is to get the existing session (if one is present and hasn't expired) and delete that before calling get_session(). Unfortunately, this didn't solve my problem as it still uses the old session for some reason. It seems that $session->delete() didn't do what I thought it would.
-------------------------------------------- login.cgi (modified) -------------------------------------------- #!/usr/bin/perl -w ... ... # new query object my $cgi = CGI->new; # Load and Delete existing session delete_existing_session($cgi); # Get a new session my $session = get_session($cgi); ... ... -------------------------------------------- delete_existing_session() -------------------------------------------- sub delete_existing_session { my $cgi = shift; my ($session, $exist_sess_id); # Query the cookie and retrieve existing session id, if exists $exist_sess_id = $cgi->cookie( "CGISESSID" ) or undef; # Load existing session $session = CGI::Session->load( $exist_sess_id ); # Delete the session $session->delete(); }
Can someone help me with this please?
Thanks, vgn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |