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

Dear Monks

I have a small problem, what I am trying to do is like a session timer, and log them out after 15 mins of inactivity. I have a perl script which updates the database 'lastactive' column, and then when the script is called it checks that column.

However, I am unsure of how to calculate the time now and the time in the database to check the difference :(

The format of the time in the database is dd/mm/yy-hh:mm:ss - but I can easily change this to whatever format is easier.

I know there are modules out there, but I would rather avoid user a module as this needs to be used on more than one machine, and would cause extra hassle to install those modules :(

Thanks in advance for any help

Replies are listed 'Best First'.
(shockme) Re: Time Question
by shockme (Chaplain) on Dec 04, 2001 at 07:34 UTC
    There are modules that would give you much tighter control, but since you have an aversion to using them.....

    Check out time(). It returns epoch time, which is number of seconds elapsed since January 1, 1970. Something like the following (untested) code should get you going:

    $current_time = time(); # 900 seconds = 15 minutes expire() if (($time_last_visited + 900) > $current_time); sub expire { ... }

    If things get any worse, I'll have to ask you to stop helping me.

Re: Time Question
by ckohl1 (Hermit) on Dec 04, 2001 at 07:40 UTC
    Depending on the database that you are storing your data in, you may have access to a function like DATEDIFF. This function will usually return the delta of two dates in a format that you choose (seconds, minutes, ...).

    Another choice is to store the epoch seconds in your table (Perl's time function). Then go back and compare your stored epoch seconds to the current epoch seconds. If the delta of the two epochs is greater than 900, then the elapsed time is greater than 15 minutes.


    Chris
Re: Time Question
by toadi (Chaplain) on Dec 04, 2001 at 14:14 UTC
    perldoc Date::Calc or perldoc Date::Manip will learn you that these are in the perl core...

    --
    My opinions may have changed,
    but not the fact that I am right

Re: Time Question
by cLive ;-) (Prior) on Dec 04, 2001 at 15:53 UTC
    Is there any reason you aren't using cookies?

    If not, then just send them a cookie with each page:

    # major "snip", but you get the idea... my $q = new CGI; my $cookie = $q->cookie(-name=>'timeout', -value=>'session id', -expires=>'+15m'); print $q->header(-cookie=>$cookie);
    then just check that cookie exists.

    .02

    cLive ;-)

      But remember that cookies are stored by the client, and as such are freely modifiable by that client.
      For this to be safe, I'd recommend sending another cookie which is a one-way hash of the cookie containing the expiration time, and some secret data known only to the server.
      For a bit more on this, check out Encrypted Storage of sensible Data in a Cookie
      A golden rule: *Never* trust the data the client gives you without doing some checks on it.
      Hope that helps.
Re: Time Question
by Spudnuts (Pilgrim) on Dec 05, 2001 at 01:57 UTC
    I've never had a problem justifing the use of modules because the inconvenience ends up cutting down the amount of time I need to spend redistributing the source to the various hosts to correct a local error that was likely handled properly by the module in the first place.