Web Hosting Talk







View Full Version : Perl Problem


George
07-16-2002, 12:00 AM
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;
}
}

Matt Lightner
07-16-2002, 12:16 AM
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;
}
}

George
07-16-2002, 12:21 AM
Matt, thank you sooooooooooooo much, the == took care of it. I took out the ". " mostly out of habit, to keep it neat