Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Counter that won't work with the IP Log

by Kage (Scribe)
on Nov 15, 2001 at 12:20 UTC ( [id://125526]=perlquestion: print w/replies, xml ) Need Help??

Kage has asked for the wisdom of the Perl Monks concerning the following question:

Okay.. I have a text counter that I am making, and it logs everyone's IP (it's supposed to log them only once..) and the time.. then it is supposed to open the IP log, compare the user's IP with all the IP's in there, if it matches, it uses -M ... > 1 to check if the timestamp is more then 1 day, which apperantly it doesn't.. what it does is just adds their IP each time they view the counter, and it adds 1 to the count.. this is the code I am working with...
#!/usr/bin/perl $count_file = "count.txt"; $ip_file = "ips.txt"; $ip=$ENV{'REMOTE_ADDR'}; $time=localtime( time ); open(COUNT,"$count_file") || die("Perl SNAFU $!"); ($count) = <COUNT>; close(COUNT); $count =~ s/\n//; open(FILE,$ip_file) || die("Perl SNAFU $!"); @data=<FILE>; close(FILE); $countem="0"; foreach $lines (@data) { chop($lines); ($ips,$times,)=split('=',$lines); if ($ip =~ /$ips/i) { if (-M /$times/i > 1) { $countem="0"; } else { $countem="1"; } # End IfTimes } else { $countem="1"; } # End IfIps } # End ForEach if ($countem) { $count++; open(COUNT,">$count_file") || die("Perl SNAFU $!"); print COUNT "$count"; close(COUNT); open(WRITE,">>$ip_file") || die("Perl SNAFU $!"); print WRITE "$ip=$time\n"; close(WRITE); } # End IfCount print "Content-type: text/html\n\n"; print "<\!--\n"; print "document.write(\"$count\");\n"; print "//-->";

Replies are listed 'Best First'.
Re: Counter that won't work with the IP Log
by mitd (Curate) on Nov 15, 2001 at 14:08 UTC
    No, No, my friend read Kanji first line -M is a FILE Operator It works on FILES not scalars. Also Kanji is a little confused in his use of localtime(time).

    localtime(time) in an array context returns a list of time elements ($sec,$min ...), In your case you are using it in a scalar context in which case it returns a formatted time string ( Thu Nov 15 03:32:13 2001 ). If this is what you want your delta date calculations will probably need the assistance of a perl module like Date::Calc.

    But there is an easier way that Kanji's example although not quite right, illustrates.

    # code frag written in long hand for clarity (I hope) my $delta_day; my $now = time(); my $oldiptime = ---from your file -- (($now - $oldiptime) >= 86400) ? ($delta_day = 1) : ($delta_day = 0);
    Your timestamps will now be in system time format but a simple three liner (hey using localtime() ) can coerce them back into human readable format.

    Lastly you can vastly simplify your script by going to your favorite Perl reference and first reading about hashes and then reading about many options for 'tieing' external hashes. Here's a teaser.

    # code fragment there's a use strict lusting above if ( exists ($IP_LIST{$ip} ) { go check delta day and stuff } else { # sdd it $IP_List{$ip} = $now; }

    good luck

    mitd-Made in the Dark
    'Interactive! Paper tape is interactive!
    If you don't believe me I can show you my paper cut scars!'

Re: Counter that won't work with the IP Log
by Kanji (Parson) on Nov 15, 2001 at 12:38 UTC

    Ignoring the other issues (use strict;, -w, formatting, etc.) in your code, I think you're a little confused on how -M works: it's meant for use with files, not arbitrary strings.

    Instead, you probably want something like...

    if ( int($times/86_400) > 1 ) { # ignores daylight savings
    Update: See mitd's response below. ( That'll teach me to post happens when I haven't had enough sleep. :)

        --k.


Re: Counter that won't work with the IP Log
by Kage (Scribe) on Nov 15, 2001 at 12:31 UTC
    shouldn't I use if (-M /$times/i > 1) { ... }? or is there an alternative method?
    "I am loved by few, hated by many, and wanted by plenty." -Kage (Alex)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://125526]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found