#1
  1. King of da Wackos
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Planet Zeekois
    Posts
    92
    Rep Power
    0

    Question Search a text file instead of geting it.


    For a page counter, I got...

    Code:
    #!/usr/bin/perl
    
    $counter = "/public_html/counter.txt";
    {
      open(NUMBER,"$counter");
      $num = <NUMBER>;
      close(NUMBER);
    
    use LWP::Simple;
    $number = get ('http://www.domain.com/counter.txt');
    $ipaddress2 = get ('http://www.domain.com/ip-address.txt');
    $ipaddress = "/public_html/ip-address.txt";
    
      if ($ipaddress2 == $ENV{'REMOTE_ADDR'})  { $num; } else { $num++; }
    
       open(NEWFILE,">$ipaddress") || die $!;
       print NEWFILE "$ENV{'REMOTE_ADDR'}\n";
       close(NEWFILE);
    
       open(NUM,">$counter") || die $!;
       print NUM "$num";
       close(NUM);
    }
    {
       print "Content-type: text/html\n\n";
    print <<EOM;
    $num visitors since 9/17/10.
    EOM
    }
    that only keeps track of the IP address of the last visitor.

    How would I edit this so in ip-address.txt it keeps the last five unique IP addresses like

    IP Address
    IP Address
    IP Address
    IP Address
    IP Address

    searches the file to see if the newest page request comes from a new visitor, or is one of the last five visitors making another page request, does the counter updating if it's a new visitor or ignores it if it's a visitor making another request, and if it's a new visitor, adds their IP address to ip-address.txt and removes the IP address generated five unique visitors ago?
    Just a coding n00bie, and is most likely a SPAMMER, and a traitor to da cause!!!

    Um, can some one give me some red??!! I got '1 points total' so I need just one negative rep to get back to being most likely a SPAMMER, and a traitor to da cause!!!

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    I guess that you'll have to store the IP addresses n an array or in a hash. Each solution has its advantages and downsides. Hash will have a faster and simpler look up, but maintaining the entry count to 5 is a biut more complicated, with an array, the lookup will be more complicated since you'll have to check each entry.

    As a side note, you should "use strict;" and "use warnings;" for any script longeer than a one-liner.

    Don't compare IP addresses with the numeric == operator but with eq.


    Code:
    if ($ipaddress2 == $ENV{'REMOTE_ADDR'})  { $num; } else { $num++; }
    would be better written:

    Code:
    $num++ if $ip_address ne $ENV{'REMOTE_ADDR'};

IMN logo majestic logo threadwatch logo seochat tools logo