Hi,
I'm writing a little ban routine for a message board.
I am having trouble with the subroutine to remove banned ip.
My code removes the bad IP entry and all values below it. I am storing the values in a text file.
All help gratefully received. I am very new to Perl so would appreciate a simple reply :)
#!/usr/bin/perl
print "Content-type: text/html\n\n";
& read_input;
$bad_ip = $FORM{'banip'} ;
unless ($bad_ip =~ /^\d+.\d+.\d+.\d+$/) {
print "<font face=Verdana size=2>That IP looks Invalid!";
print"<br><br>\n";
print "<div align = center><a href=\"javascript:history.go(-1)\">Go B
+ack and Try Again</a></div>";
exit;
}
unless (-e "ip.txt") {print "There are no banned IP's<br>";
exit;
}
open (IP, "<ip.txt");
@ip= <IP>;
close (IP);
open (IP, ">ip.txt");
foreach $_(@ip) {
chomp $_;
if (/$bad_ip/) {next};
chomp$_;
print IP "$_\n";
close (IP);
}
print "All Done!<br>";
print"<br><br>\n";
print "<div align=center><a href=cp.htm>Back to the menu here</a>";
sub read_input {
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.