Home: Perl Programming Help: Intermediate:
(How) Can I shorten a logged IP?



mwaf
Deleted

Feb 9, 2000, 5:12 AM


Views: 6896
(How) Can I shorten a logged IP?

I've written a poll program and I've got this IP logging thing in the script in it for "protecting" visitors from voting twice.

open(IN,"ip");
$ip = <IN>;
close(IN);

if ($ip =~ /$ENV{'REMOTE_ADDR'}/) {
#print "You've allready voted"
}
else {
$ip = "$ENV{'REMOTE_ADDR'} : $ip";
open(OUT,">ip");
print OUT "$ip";
close(OUT);
#the rest of the poll script
}

This will make my "ip" file look something like this:

195.74.4.237 : 195.74.4.232 : IP list end

Now these two IPs are both mine, the difference between them is the last three numbers. Those numbers are randomly (or maybe in some order) given to me when I connect the my ISP, and I don't think my ISP is the only one doing this. So my script part "protecting" visitors to vote twice really works only during one connection establishment.

So, what I would like to know is how I could shorten (is this BTW correct english?) the IP by removing everything after the last dot ("."). Then I could combine that with $ENV{'HTTP_USER_AGENT'} to get a working double voting protection, not prefect of course, but better than the one I have now.

I'm sorry to post this here if you think this a Beginner question.....


Cretep
User

Feb 9, 2000, 5:29 AM


Views: 6896
Re: (How) Can I shorten a logged IP?

What about using cookies in stead of there IP address. Just a suggestion.

Regards

Peter Crouch


mwaf
Deleted

Feb 9, 2000, 5:35 AM


Views: 6896
Re: (How) Can I shorten a logged IP?

>What about using cookies in stead of there IP address.
....well, I don't know anything about cookies and also people necessarily don't have cookies turned on, I don't at least.


Cretep
User

Feb 9, 2000, 5:56 AM


Views: 6896
Re: (How) Can I shorten a logged IP?

Visit http://www.conservatives.net/atheist/scripts/
there are some great implementations of cookies in his scripts.

Regards

Peter Crouch


Jasmine
Administrator

Feb 9, 2000, 9:30 AM


Views: 6896
Re: (How) Can I shorten a logged IP?

To get only the first 3 sections of the ip#, you can use this code:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my $shortened;
my @ip = split(/\./,$ENV{'REMOTE_ADDR'});
for ($i=0; $i<=2; $i+=1){
$shortened .= $ip[$i] . '.';
}
chop $shortened; # get rid of the last .
</pre><HR></BLOCKQUOTE>

Of course with Perl there's more than one way to do it, but this is the first that came to mind.


Cure
User

Feb 9, 2000, 8:18 PM


Views: 6896
Re: (How) Can I shorten a logged IP?

Hi,

Here's another way to get only the first 3 sections of the ip#.

my @ip = split(/\./,$ENV{'REMOTE_ADDR'});
pop @ip;
@ip = join('.',@ip);

Cure


mwaf
Deleted

Feb 10, 2000, 12:33 AM


Views: 6896
Re: (How) Can I shorten a logged IP?

Thanks Jasmine and Cure!
Both versions worked well and if you want to know I use Cure version due to it being slightly simpler :-)


Cure
User

Feb 10, 2000, 7:25 AM


Views: 6896
Re: (How) Can I shorten a logged IP?

Your Welcome mwaf.

Cure


Red Dragon
Deleted

Feb 11, 2000, 8:41 PM


Views: 6896
Re: (How) Can I shorten a logged IP?

IP tracking for your purpose (or any other) has its limited usefulness. The example you gave of recieving different ips is perfect. You see there that you can merly dial up again and recieve a different ip (dynamic ips from an available pool that your isp has). This is extremly popular with isps because of the limited number of ips that the internet has currently available. There is another flip side to your delima with the ip as well. If you hang up and someone else at your isp dials up and gets your previously assigned ip then they will not be allowed to vote on this poll. Cookies too have their limitations. Simply put, if the person doesn't have cookies on or manually removes a cookie they can vote twice. If you want to do all you can to prevent stuffing your ballot box, I recommend a combination of the two processes. One, set a cookie if possible and check for this cookie when someone votes and two log ips with a time and set a time frame like 12 hours before that ip can vote. This is a popular method with many of the ad programs you see on the net to prevent padding of visits to a site or clickthroughs. I hope this helps in your efforts to prevent padding of your poll program.


Cure
User

Feb 26, 2000, 11:03 PM


Views: 6896
Re: (How) Can I shorten a logged IP?

Hi mwaf

My friend borderline showed me another way to get rid of the last 3 digits of an ip address.

map {$cure.=$_}join'.',(split '\.', $ip)[0,1,2];


japhy
Enthusiast

Feb 27, 2000, 6:41 AM


Views: 6896
Re: (How) Can I shorten a logged IP?

Well, if we're going to play Perl Golf with "how do I get the first three sections of an IP address?", then I offer these two:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$short = substr $IP, 0, rindex($IP, ".");
# or
($short = $IP) =~ s/\.[^.]+$//;
</pre><HR></BLOCKQUOTE>

In tests, the substr+rindex method is faster.