CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Show members on-line

 



Harris7357
newbie

Oct 3, 2001, 11:58 PM

Post #1 of 2 (729 views)
Show members on-line Can't Post

I use Apache and would like to find a script that will display on-line members on my web pages. Is there anything that will make a on-line list when members log in and then remove them when they don't access a page after a certain period of time? Thanks



yapp
User

Oct 18, 2001, 12:12 AM

Post #2 of 2 (717 views)
Re: Show members on-line [In reply to] Can't Post

I've got such code inserted in my forum, at http://www.cool-programming.f2s.com.

Here is a short preview of the code however.
You might not know some extra functions in detail, but it's property clear what's happening here.
I've also left out some unimportant code.

All records will be read, erased and written back when they are still valid.
The record that contains the current user will be written at the end, so everything is sorted by date. We store member links into a hash, and sort that later in a different way. We want to display the user list sorted by name.


Code
# Some variables defined elsewhere: 
# $XForumUser is the current member logged in. It's password is already validated.
# $XForumUserIP = $ENV{'REMOVE_ADDR'} || ''
# $MEMBER_TTL = 5 * 60; # 5 minutes.


{
my %MemberLinks;
my $EpochSecs = time();
my $Name = ($XForumUser ne '' ? &dbGetMemberName($XForumUser) : '');

# Open for READ/WRITE, create it not exists.
if(sysopen(TTLDB, "$DATA_FOLDER/visitors.rct", O_RDWR|O_CREAT, 0666))
{
# Lock and get the contents when locked
flock(TTLDB, LOCK_EX);
my @Visitors = <TTLDB>;

# Clear the database, so we can rewite data into it.
seek(TTLDB, 0, 0) or die "Can't reinit for write of visitors: $!";
truncate(TTLDB, 0) or die "Can't rewrite visitors: $!";

foreach(@Visitors)
{
# Remove \n and split the records, separated by pipes
chomp;
my ($TTL, $IP, $Member, $Name) = split(/\|/);

if($TTL && ($XForumUserIP ne $IP) && (($EpochSecs - $TTL) <= $MEMBER_TTL) && ($XForumUser eq '' || $Member ne $XForumUser))
{
# Put back the record because it's still valid.
print TTLDB "$TTL|$IP|$Member|$Name\n";

# Add guests, or add link to hash
if ($Member eq '' || $Name eq '') { $Guests++; }
else
{
$MemberLinks{$Member} = qq[<A href="$THIS_URL?show=member&member=$Member">$Name</A>, ];
}
}
}

# Write the last record (yourself)
print TTLDB "$EpochSecs|$XForumUserIP|$XForumUser|$Name\n";
flock(TTLDB, LOCK_UN);
close(TTLDB);
}

if($XForumUser eq '' || $Name eq '') { $Guests++; }
else { $MemberLinks{$XForumUser} = qq[<A href="$THIS_URL?show=member&member=$XForumUser">$Name</A>, ]; }

foreach my $Member (sort { $MemberNames{$a} cmp $MemberNames{$b} } keys %MemberLinks)
{ $Visitors .= $MemberLinks{$Member}; }

if ($Guests == 0) { $Guests = 'no guests'; }
elsif ($Guests == 1) { $Guests .= ' guest'; }
elsif ($Guests >= 2) { $Guests .= ' guests'; }
}

...

print "$Visitors $Guests";


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives