Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Checking users online

by kidd (Curate)
on Jul 02, 2002 at 15:29 UTC ( #178882=perlquestion: print w/replies, xml ) Need Help??
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'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178882]
Approved by Snuggle
help
Chatterbox?
[ambrus]: choroba: and for perlmonks (but not for everything2), you don't even need a cookie, you can just send a username and password parameter in every request, and this is even documented in What XML generators are currently available on PerlMonks?
[holli]: i admire you choroba, if i had to work at such a place, i wouldn't last long. as bosses don't like if someone calls them clueless idiots
[ambrus]: (The cookie format is not documented anywhere afaik, but it's trivial to reverse engineer even without being a pmdev.)
[holli]: or "ignorant bricks" (that is not a typo)
[choroba]: holli The money wasn't bad :-)
[choroba]: Discipulus Try changing the URL constants to something else

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-10-24 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (289 votes). Check out past polls.

    Notices?