Results 1 to 3 of 3

Thread: Perl Problem

  1. #1
    Join Date
    Sep 2001
    Location
    New York City
    Posts
    135

    Perl Problem

    Hello all,

    I am having trouble getting a little snippet of code to work properly. Bear in mind, the same code has worked before it is:

    The script is to log the IP and compare it to a list of IPs. When I compare it though, it returns as unique each time, even though the IP is the same(yes, I checked for this). I have tried many ways around it like =, eq, =~ and ne but to no avail, I always get an inconsistant result. Any help would be greatly appreciated.

    sub new {


    $pro = $formdata{'pro'};

    $IP = $ENV{'REMOTE_ADDR'};
    $IP =~ s/\.//g;

    open (LOG, "<$pro.ip");
    flock (LOG,2);
    @lines = <LOG>;

    foreach $lines(@lines) {
    if ($lines = $IP) {
    print "Content-type: text/html\n\n";
    print "You have already rated this script";
    $proceed = "n";
    }
    }
    close (LOG);

    if ($proceed ne "n") {

    open (FILE, ">$pro.ip");
    flock (FILE,2);
    print FILE "$IP\n";

    foreach $lines(@lines) {
    print FILE "$lines";
    }
    close (FILE);
    &procnew;
    }
    }

  2. #2
    Few problems that I can spot right away:
    sub new {


    $pro = $formdata{'pro'};

    $IP = $ENV{'REMOTE_ADDR'};
    $IP =~ s/\.//g; # Why are you stripping the '.'s out of the IP address? That means that 1.2.3.45 will be the same as 1.2.34.5

    open (LOG, "<$pro.ip");
    flock (LOG,2);
    @lines = <LOG>;

    foreach $lines(@lines) {
    if ($lines = $IP) { # the equal sign is not used for comparing.
    # You want to use either == (for comparing intigers), or eq (for comparing strings) or a regex like $lines =~ /^\s*$IP\s*$/

    print "Content-type: text/html\n\n";
    print "You have already rated this script";
    $proceed = "n";
    }
    }
    close (LOG);

    if ($proceed ne "n") {

    open (FILE, ">$pro.ip");
    flock (FILE,2);
    print FILE "$IP\n";

    foreach $lines(@lines) {
    print FILE "$lines";
    }
    close (FILE);
    &procnew;
    }
    }
    Matt Lightner - http://www.mattlightner.com/
    - First initial to the last name at the mail service provided by the world's largest search engine
    - Founder and CEO (Former) Site5.com, sold in 2008
    - Really honestly wants to be a good WHT citizen but can never remember all the correct etiquette. Mods, sorry in advance

  3. #3
    Join Date
    Sep 2001
    Location
    New York City
    Posts
    135

    Thank you

    Matt, thank you sooooooooooooo much, the == took care of it. I took out the ". " mostly out of habit, to keep it neat

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •