Click to See Complete Forum and Search --> : Form Script


jake_604
09-01-2003, 01:21 PM
I have a piece of CGI code, shown below. I would like to add something to it that once someone has used the form, their IP will be blocked, so they cannot use it again. I am not very technical and do not know how I can do this, so can anyone please help me?

Thank you


print "Content-type: text/html\n\n";

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);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value; }

@months = ("January","February","March","April","May","June","July",
"August","September","October","November","December");

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900; $month = $mon + 101; $day = $mday + 100; $date = "$months[$mon] $mday, $year";
$time = "$hour\:$min\:$sec";

@fields = ('reason','username','name','email','rating','message');

foreach $req(@required) {
if (! $FORM{$req}) {
print qq~
You have missed out some fields that are required.
<P>
Please go <a href="javascript:history.go(-1);">back</a> and try again.
~;
exit;
}
}

if ($FORM{'email'}) {
unless ($FORM{'email'} =~ /\w+@\w+.\w+/) {
print qq~
You have entered an email address in a incorrect format.<br><br>
Please go <a href="javascript:history.go(-1);">back</a> and try again.
~;
exit;
}
}

open(MAIL,"|$Mail_Program") || &noprogram;
print MAIL "To: $E_Mail\n";

if (! $FORM{'email'}) {
print MAIL "From: $From_Address\n";
} else {
print MAIL "From: $FORM{'email'}\n";
}

if (! $FORM{'subject'}) {
print MAIL "Subject: $Subject\n\n";
} else {
print MAIL "Subject: $FORM{'subject'}\n\n";
}

print MAIL "Submitted at $time on $date...\n\n";

foreach $field(@fields) {
if ($FORM{$field}) {
if ($field eq "main") {
$FORM{'main'} =~ s/\Q\n\E/\n/g;
print MAIL "$field = $FORM{'main'}\n";
} else {
print MAIL "$field = $FORM{$field}\n";
}
}
}
close(MAIL);

if ($Send_Reply eq "1") {
open(REPLY,"$Reply_Template");
@reply = <REPLY>;
close(REPLY);

foreach $line(@reply) {
$line =~ s/\Q\n\E/\n/g;
$fiction .= $line;
}

open(MAIL,"|$Mail_Program") || &noprogram;
print MAIL "To: $FORM{'email'}\n";
print MAIL "From: $E_Mail\n";
print MAIL "$fiction\n\n";
print MAIL "Admin\n";

close(MAIL);
}

print qq~
<HTML>
<HEAD>
<META HTTP-EQUIV=Refresh CONTENT=0;URL="$Return_Link">
</HEAD>

<BODY></BODY>
</HTML>
~;
exit;

sub noprogram {
print qq~
Sendmail program failed to open!<br>
Message NOT sent.<br>
Please contact the webmaster about this problem.
~;
exit;
}

jimr451
09-05-2003, 07:53 AM
Well, the basic strategy would be something like this:

- Maintain a file of IP addresses that have posted

- (Next time the script runs) Open the file

- while not at the end of the file

- Read a line
- Compare it to the current IP address of the sender
- If a match is found, send a reject message

-If no match is found, log the IP and accept the submission


* keep in mind, limiting by IP address may not accomplish what you want - for many people on dialup / DSL, IP's are assigned dynamically, so the next time they log on or reboot their computer, they might get a new IP.

Hope this helps.

-Jim