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

I'm using standard CGI sessions with 2 fields in my database and I was wondering if there is an easy way of telling how old the sessions are so I can delete the old expired ones.

Title edit by tye

Replies are listed 'Best First'.
Re: sessions
by The Mad Hatter (Priest) on Jun 05, 2003 at 22:59 UTC
    Assuming you're using CGI::Session, use the expire method. Straight from the doc:
    # expire the session itself after 1 idle hour $session->expire('+1h');

    Update Made 'doc' a link to the relevant section of the documentation

      Sorry I didn't include enough detail in my first post. I'm using CGI::Session and I have my sessions expire set at 30 minutes:

      $session->expire('+30m')

      My problem is that my MySQL database keeps filling up with sessions that I want to delete after they are expired. I was thinking about parsing the epoc out of the a_session field, but I am looking for a more elegant way to clean the expired sessions out of the database.

      Thanks
        I see. I haven't used CGI::Session before, but perhaps you could use a combination of $session->atime, $session->expire, localtime, and $session->delete to get something like this pseudo-code:
        $session->delete if (($session->atime + $session->expire) < localtime) +;
Re: Age of CGI sessions?
by LAI (Hermit) on Jun 06, 2003 at 13:13 UTC

    well, you've got to find the date in the database somehow. Parsing out the timestamp is annoying, indeed. The way I would probably do it would be to have another field in the database of type datetime, and every time the session updates something it sets that field to now(). That way you can easily delete from tablename where.

    If changing what goes into the database is not an option, then you're probably stuck parsing out the date... unless you do something like create temp files on disk holding (or named after) the values of the primary key of your table.

    LAI

    __END__