Gossamer Forum
Home : General : Perl Programming :

Whos online script mod

Quote Reply
Whos online script mod
Hi,

Could someone help and tell me how to modify this script so it show the IP for each user that is on-line? (I know very little about Perl)

my $time_now=time;
my $visitor_ip=$ENV{'REMOTE_ADDR'};
my $visitors="1";
my(@new);

open(DATA, "$data_file")|| file_error("Error reading <u>$data_file</u> : $!");
my @data=<DATA>;
close(DATA);

foreach my $data_value (@data){
my($logged_ip, $logged_time) = split(/\|/, $data_value);
my $time_diff=$time_now-$logged_time;
push(@new, $data_value) if ($time_diff <= $timeout);
}

open(UPDATED, ">$data_file")|| file_error("Error writing to <u>$data_file</u> : $!");
foreach my $record (@new) {
my($logged_ip, $logged_time) = split(/\|/, $record);
print UPDATED "$record" if ($visitor_ip ne $logged_ip);
$visitors++ if ($visitor_ip ne $logged_ip);
}
print UPDATED "$visitor_ip|$time_now\n";
close(UPDATED);

sub file_error {
print "Content-type: text/html\n\n";
print "<font face=Verdana size=2>@_</font>";
exit;
}

print "Content-type: text/html\n\n";
print "$visitors";

exit;
Quote Reply
Re: [sooke] Whos online script mod In reply to
Hm... try this (untested).
Code:
my $logt = 1800; #30 minutes
my $time = time;
my $hits = 1;
my @ip = ($ENV{REMOTE_ADDR});

open (DATA, "ip.log") or die $!;
open (TMP, ">tmp.log") or die $!;
flock TMP, 2;
while (<DATA>) {
my @rec = split /\|/;
($rec[1] - $time < $logt) and ($rec[0] ne $ENV{REMOTE_ADDR}) or next;
push @ip, $rec[0];
print TMP (join "|", ($rec[0], $time)) . "\n";
$hits++;
}
print TMP (join "|", ($ENV{REMOTE_ADDR}, $time)) . "\n";
close TMP;
close DATA;

rename "tmp.log", "ip.log";

print "Content-type: text/html\n\n";
print "visitors: $hits<br>\n" . join("<br>\n", @ip);

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] Whos online script mod In reply to
Awesome Philip! It works, thanks.

Quote:


open (TMP, ">tmp.log") or die $!;


Is there really meant to be a ">" in ">tmp.log"?

Thanks againSmile
Quote Reply
Re: [sooke] Whos online script mod In reply to
Yes :)

> tells the script to open the file for writing.
Quote Reply
Re: [sooke] Whos online script mod In reply to
Sweet. I like it when stuff works on the first shot without any bugs Smile

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] Whos online script mod In reply to
It only happens rarely for me :(

Last edited by:

Paul: Apr 29, 2002, 7:36 AM
Quote Reply
Re: [Paul] Whos online script mod In reply to
Dont say that Paul, your AdCredits global you just wrote for me is sweet!
Quote Reply
Re: [sponge] Whos online script mod In reply to
Hi Sponge...... sorry to bring the news, but I have a couple of confirmed unique IP's online and it still is only showing 1 Unsure

Any ideas?

Right now, it only shows my IP and 1 visitor, but noone else's.

Thanks
Quote Reply
Re: [sooke] Whos online script mod In reply to
okay there were two problems I found. The first is that the time difference was being compared incorrectly, second, the time was being replaced for all ip's, not just the ip using the script. There may still be another Frown

Code:
use strict;

my $logt = 1800;
my $time = time;
my $hits = 1;
my @ip = ($ENV{REMOTE_ADDR});

open (DATA, "ip.log") or die $!;
open (TMP, ">tmp.log") or die $!;
flock TMP, 2;
while (<DATA>) {
chomp;
my @rec = split /\|/;
($time - $rec[1] < $logt and $rec[0] ne $ENV{REMOTE_ADDR}) or next;
push @ip, $rec[0];
print TMP (join "|", ($rec[0], $rec[1])) . "\n";
$hits++;
}
print TMP (join "|", ($ENV{REMOTE_ADDR}, $time)) . "\n";
close TMP;
close DATA;

rename "tmp.log", "ip.log";

$, = "<br>\n";

print "visitors: $hits" , @ip;

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] Whos online script mod In reply to
Hi Phillip,

Looks good, thanks.... though I am getting an internal server error now.

Are there any typos that you can see?

I have path to perl at the top correctly.
Quote Reply
Re: [sponge] Whos online script mod In reply to
I changed the last two lines to:




print "Content-type: text/html\n\n";
print "visitors: $hits<br>\n" , @ip;

And this removed the error.

Just waiting for people to show up online again to test!

Thanks again

Smile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [sooke] Whos online script mod In reply to
oops. to test the program i had to run it from command prompt and manually substitute REMOTE_ADDR with different IP's since I'm on a dedicated line with a dynamic IP that only changes once a week. printing the content type was just annoying since it isn't needed from when run from command line.

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] Whos online script mod In reply to
wow, one of these hard to set up situatations eh?

Well, all my traffic died right off as soon as I installed the script! go figure, so I have not been able to see multiple people on line yet....
Quote Reply
Re: [sponge] Whos online script mod In reply to
Were you able to get the script to show more than 1 ip at once??

I just tried going to my site with multiple instances of megaproxy and nothing showed up...hmmmmm
Quote Reply
Re: [sooke] Whos online script mod In reply to
Don't proxies cause anonymity? Look at ip.log and post what it shows.

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] Whos online script mod In reply to
Yeah, only my IP showing up.... I just had another visitor (registerd) come on, and it still only shows my ip.... weird.

The ip.log just has one line in it:

my ip | 1020126980