garrison has asked for the wisdom of the Perl Monks concerning the following question:

I'm using mod_perl2 + Apache::Session::MySQL and I'm wondering what to do with stale sessions. Does Apache::Session do any cleanup or do I need to explicitly tied(%session)->delete;?

I'm storing session IDs in cookies for 60 days, so I'd like to limit sessions similarly. I could certainly run a cron for this, but is there a better way?

Replies are listed 'Best First'.
Re: Apache::Session cleanup
by EvanCarroll (Chaplain) on Sep 18, 2005 at 05:45 UTC
    No, I regret to inform you there is no better way. You can and should include add a data stamp in every session entry, you can then go back through them letter with another script.

    The whole Apache::Session::* is very undocumented, though I have read the src myself a few times. By default Apache::Session::DBI will store using a frozen copy of a Base64 bit encoded hash: This, thankfully, is easily reversible.

    Here is some code that should show you the values you have stored, you can use this and a few simple sql tables, to move the data out of the frozen/base64 encoded hash into a real sql table.
    use Storable q/thaw/; use MIME::Base64; use Data::Dumper; use DBD::Pg; use Carp; use Apache::Session::Serialize::Base64 qw/unserializie/; my $dbh = DBI->connect("dbi:Pg:dbname='TABLE_NAME'",q/postgres/,"",{Au +toCommit =>0}) || croak "Could not Connect to DB $DBI::errstr"; my $sth = $dbh->prepare(qq{ SELECT "a_session" FROM sessions }); $sth->execute(); + while ( my $col = ($sth->fetchrow_array)[0] ) { my %hash = %{ thaw(MIME::Base64::decode( $col ) ) }; while ( my ($k, $v) = each %hash ) { print "$col :\t $k => $v\n"; } }


    Evan Carroll
    www.EvanCarroll.com
Re: Apache::Session cleanup
by CountZero (Bishop) on Sep 18, 2005 at 14:03 UTC
    I haven't studied the source of Apache::Session, but if your store is MySQL I think you can add a timestamp in the record which gets automatically updated with the current time everytime you update the record (just reading the record would not update it). Deleting "old" sessions then is easy and you don't have to unserialize the session-data to do so.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law