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


In reply to Checking users online by kidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.