Gossamer Forum
Home : General : Perl Programming :

time problem

Quote Reply
time problem
hey, i have a bit of a problem, im using session which are suppose to timeout out after an hour. what i need to know is if there is a specific method of checking the old time against the current time. when a sesseion is created the time is added to a database in this way:

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
$0 = 0;
if (length($hour) == 1) { $hour = $0.$hour; }
if (length($min) == 1) { $min = $0.$min; }
if (length($sec) == 1) { $sec = $0.$sec; }
if (length($day) == 1) { $day = $0.$day; }
if (length($mon) == 1) { $mon = $0.$mon; }

@list = split(/(?=\d)/, $year);
$year = "20".$list[1].$list[2];

$time = $year."-".$mon."-".$day." ".$hour.":".$min.":".$sec;
$add_session_statement= "insert into session values('', '$row_ary[0]', 'y', '$time')";
$add_session = $dbh->do($add_session_statement);
Quote Reply
Re: [pedge] time problem In reply to
No need for all that.
Add a timestamp(14) column to your session table.
No need to add any value to it as it auto sets to current date time.

CREATE TABLE session (
session bigint(15) DEFAULT '0' NOT NULL ,
tim timestamp(14) ,
PRIMARY KEY (session)
);

To clean out redundent sessions use -

my $ses_time = 3600; #1 hour in seconds
my $sys_time = time();
my $query = qq!
DELETE FROM session WHERE UNIX_TIMESTAMP(tim) + '$ses_time' < '$sys_time' LIMIT 100
!;
$dbh->do($query) or &cgierr("Unable to delete from session. Reason: $DBI::errstr.");

It's easier to work in seconds.

Bob