Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Users on a Forum

Status
Not open for further replies.

NateBro

Programmer
Sep 1, 2004
233
US
I have created a little forum,
and i am trying to figure out how to remove a username from a text based DB, i can add it when they log on. but how do i remove it if they just close the window, without logging off? i was thinking about using javascript, but i want it to be server side. is there a way to, edit a file right before the page is unloaded?

any help would be great, i have only just started CGI so i tend to think more how i would do it in javascript, instead of PERL.

thanks,
~Nate_Bro
 
you remove them based on a time limit. Whenever the file is opened check all the names against the last time there waa activity, if the time exceeds so many minutes, days, weeks (whatever) you delete it. CGI::Session is made for this type of thing (and plenty more).
 
ok, i'll try to figure how to use the CGI::Session,
thanks,
~Nate_Bro
 
hmmmmm.... i can't seem to think of how to get it to work for what i want to do.

i'm just wanting a very simple code for this one.

something like this..

Code:
## Opens the Active Users File ##
open (DATA, "file.dat");
my @data = <DATA>;
close (DATA);

 
## Gets $username
 $username = param('username');
  
 
## Removes the $username line ??  
 $nothing = "";
 chomp($nothing);
  

## Reads each line till it finds $username
for ($i = 0; $i <= $#data; $i++){

	
	## If $username found in file...
	if ($data[$i] =~ /$username/) {

	
	## Replace the line with $nothing
	## and the line is removed completely ??
	$data[$i] =~ s/$username/$text/g;
	}
}

## Writes new information to the file
## with the username removed
open (DATA, "> file.dat");
print DATA "@data";
close (DATA);

but i'm not sure how to call it right before the person closes the window, or moves to another page. any ideas?
 
you can't. You should be putting a time stamp in the line for each user. This records the time of their last activity. Now, anytime the file is opened, all users are checked against the current time and the time stamp from their last activity. If the time alloted is exceeded, you delete that user from the file. What do the lines in file.dat look like?
 
the lines i guess are just going to look like...

name_one
name_two
name_three

...I'll write the whole program out in a min.

i might give you a better idea of what i'm trying to do.
 
Ok here is the whole code...


Code:
#!/usr/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);


## Gets $username ##
 $username = param('username');


#######           ########
#######  LOG IN   ########
#######           ########



## Opens the Active Users File ##
open (DATA, "file.dat");
my @data = <DATA>;
close (DATA);


## Opens the Active Users File & writes to file ##
open (DATA, "> file.dat");

## adds $username to the top of the list ##
print DATA "$username\n";

## prints the rest of the list ##
print DATA "@data\n";
close (DATA);


## prints HTML text ##
print "Your loged in and have been added to the active list!";




#######           ########
#######  LOG OUT  ########
#######           ########

## Opens the Active Users File ##
open (DATA, "file.dat");
my @data = <DATA>;
close (DATA);

 
## Removes the $username line ?? ##
 $nothing = "";
 chomp($nothing);
  

## Reads each line till it finds $username ##
for ($i = 0; $i <= $#data; $i++){

	
	## If $username found in file... ##
	if ($data[$i] =~ /$username/) {

	
	## Replace the line with $nothing        ##
	## and the line is removed conpleatly ?? ##
	$data[$i] =~ s/$username/$text/g;
	}
}

## Writes new information to the file ##
## with the username removed          ##
open (DATA, "> file.dat");
print DATA "@data";
close (DATA);


if its to hard, i can just do it in javascript, it will just be slower, but it will work.
 
I would do something more like this:

Code:
#!/usr/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print header;


## Gets $username ##
$username = param('username');


#######           ########
#######  LOG IN   ########
#######           ########

if (param('login') {
   ## Opens the Active Users File ##
   open (DATA, ">>file.dat") or die "$!";
   flock (DATA,2);#<-- lock the file if your system supports flock
   print DATA "$username|".time."\n";
   close (DATA);

   print "You are logged in and have been added to the active list!";
}

#######           ########
#######  LOG OUT  ########
#######           ########

if (param('logout') {
   ## Opens the Active Users File ##
   open (DATA, "file.dat") or die "$!";
   my @data = <DATA>;
   close (DATA);
   chomp(@data);

   open (DATA, ">file.dat") or die "$!";
   flock (DATA,2);
   for (@data) {
      my ($user,$time) = split(/\|/);    
      next if ($user eq $username);
      next if (time > $time+3600); #3600 = 10 minutes 
      print DATA "$_\n";    
   }
   close(DATA);
}
this line removes a user that logs out:

next if ($user eq $username);

this line removes any user whos time has expired:

next if (time > $time+3600); #3600 = 10 minutes
 
Ok, thanks. i think that will work great!

~Nate_Bro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top