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

Hi all,

I have a list of session ids with timestamps (using the Perl time function). I want the sessions to expire if they have lasted longer than two hours. So, I have a cron job that runs every two hours: It gets the current timestamp (using the time function). Then for each session id, it calculates the difference between the session's timestamp and the current timestamp. If the hours difference > 2, then I kill the session. Calculating this time difference is my biggest problem. How do I do this? What is the format of the time function? Is there a Perl function that can convert the timestamp to all minutes or all hours? Please help! Thanks.

Title edit by tye

Replies are listed 'Best First'.
Re: Timestamps
by Paladin (Vicar) on Feb 26, 2003 at 05:55 UTC
    According to the perldoc for time:
    time Returns the number of non-leap seconds since whatever t +ime the system considers to be the epoch (that's 00:00:00, Janu +ary 1, 1904 for Mac OS, and 00:00:00 UTC, January 1, 1970 for +most other systems). Suitable for feeding to "gmtime" and " +local- time".

    So it is in seconds. To find the difference between your timestamp and the current time, you just subtract.

    if ( (time() - $timestamp) > (60 * 60 * 2) ) { # Do what you need to expire the session }
Re: Timestamps
by graff (Chancellor) on Feb 26, 2003 at 05:57 UTC
    You should start by reading the output of "perldoc -f time":
    time Returns the number of non-leap seconds since what­ ever time the system considers to be the epoch (that's 00:00:00, January 1, 1904 for Mac OS, and 00:00:00 UTC, January 1, 1970 for most other sys­ tems). Suitable for feeding to "gmtime" and "localtime". For measuring time in better granularity than one second, you may use either the Time::HiRes module from CPAN, or if you have gettimeofday(2), you may be able to use the "syscall" interface of Perl, see perlfaq8 for details.
    So, based on this info, you probably want to kill a session when the difference between its start time and the current time is greater than (2*60*60), which is the number of seconds in two hours.
Re: Timestamps
by caedes (Pilgrim) on Feb 26, 2003 at 08:00 UTC
    Instead of running the cron job, I would just do the test every time they access the page. At first this might seem wasteful, but it really isn't since you already have to get the session id for each access. It's just a matter of comparing the session id and the timestamp each time. Only if the session id matches and the timestamp is within bounds do you complete the authentication.

    You can really replace the cron job with a single line of perl code in your existing script.

    -caedes

Re: Timestamps
by rob_au (Abbot) on Feb 26, 2003 at 11:36 UTC
    I would concur with the suggestion made by caedes above.

    A far better solution I think would be to make use of the Cache::Cache function for the storage of session information - This caching package includes a facility to expire entries and to automatically purge expired entries at set intervals or upon cache access (set or retrieval).

    For example, your cache interface could look as simple as the following:

    use Cache::FileCache; my $cache = Cache::FileCache->new ( 'auto_purge_on_get' => 1, 'default_expires_in' => '2 hours' ); . . $cache->get( $key ); if ( !defined $cache->get( $key ) ) { # Create new session } $cache->set( $key, $value );

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000110101"))'