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

I've created a CGI wich checks for users who are online. Here is how it works:

1.When a user logins write's into a file <online.log> a string like this "user%%020703234516" wich is year/month/day/hour/minute/second
2.Opens up a page with frames, one wich is a hidden frame in wich the CGI refreshes every minute.
3.When the CGI is called "online.cgi" checks the list and sees if a user has expired, in that case it just delete's it.
4.On the other hand it update the date string.

The reason Im doing this its because I dont want users who dont log out te be kept online. Because if your computer shitdowns and you want to access again you wouldn't be able to do so, because you'll seem to be online.

I hope someone here could help me, either telling me what's the problem with my code, or giving me some ideas.

Anyway here is my code:

#!/usr/bin/perl -w use strict; use CGI; my $form = CGI::new(); my $user = $form->param('user'); my $time = &getTime; &getFile($user,$time); &getOffline; &printOutput($user); sub getFile{ my $user = shift; my $time = shift; open(FILE, "online.log"); flock(FILE, 1); my @lines = <FILE>; close(FILE); open(FILE, ">online.log"); flock(FILE, 2); foreach my $line(@lines){ chomp($line); my($ruser, $expire) = split("%%", $line); if($user eq $ruser){ print FILE "$user" . "%%" . "$time"; }else{ print FILE "$line\n"; } } close(FILE); } sub getTime{ my $feb_days; my ($sec, $min, $hour, $mday, $mon, $year) = (localtime(time))[0,1,2,3 +,4,5]; $mon++; $min = $min + 1; if($min > 59){ $min = $min - 60; $hour++; } if($hour > 23){ $hour = $hour - 24; $mday++; } if ($year % 4 != 0 || ($year % 100 == 0 && $year % 400 != 0)) { $feb_days = "28"; } else { $feb_days = "29"; } if ($mon == 2 && $mday > $feb_days){ $mon++; $mday = $mday - $feb_days; } if ((($mon == 1) || ($mon == 3) || ($mon == 5) || ($mon == 7) || ($mo +n == 8) || ($mon == 10) || ($mon == 12)) && $mday > 31){ $mon++; $mday = $mday - 31; } if ((($mon == 4) || ($mon ==6) || ($mon == 9) || ($mon == 11)) && $md +ay > 30){ $mon++; $mday = $mday - 30; } if ($mon > 12){ $year = $year + 1; $mon = $mon - 12; } $min = "0" . $min if (length($min) == 1); $hour = "0" . $hour if (length($hour) == 1); $mon = "0" . $mon if (length($mon) == 1); $mday = "0" . $mday if (length($mday) == 1); $sec = "0" . $sec if (length($sec) == 1); $year += 1900; $year = substr($year,2,2); my $t = "$year" . "$mon" . "$mday" . "$hour" . "$min" . "$sec"; return($t); } sub getOffline{ my $newTime = &getTime; open(FILE, "online.log"); flock(FILE, 1); my @lines = <FILE>; close(FILE); open(FILE, ">online.log"); flock(FILE, 2); foreach my $line(@lines){ chomp($line); my ($user, $rtime) = split("%%", $line); print FILE "$line\n" if ($newTime < $rtime); } close(FILE); } sub printOutput{ my $nuser = shift; print "Content-type: text/html\n\n"; print<<EOF; <HTML> <BODY> <form name="online" method="post"> <input type="hidden" name="user" value="$nuser"> </FORM> <script> document.online.action="http://www.baboonsoftware.com/cgi-bin/online.c +gi"; setTimeout("document.online.submit()",60000); </script> </BODY> </HTML> EOF }

Edited: ~Wed Jul 3 17:45:32 2002 (GMT), by footpad:
Added <READMORE> tag, per Consideration

Replies are listed 'Best First'.
Re: Checking users online
by grinder (Bishop) on Jul 02, 2002 at 18:08 UTC
    For your date formatting...

    $min = "0" . $min if (length($min) == 1);

    ... I would recommend using sprintf instead:

    $min = sprintf "%02d", $min;

    For your date calculations (adding one time value to another), you can do it much more succinctly with Date::Calc.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'