Hopefully someone can help. I have a simple CGI script that takes form data from a webpage and writes it to a text file. I need to alter the script so that it the script will check the file and see if the entry I am about to write already exists and then write it if it does not exist or exit if it does. Has anyone done this before that could help? Here is the script as it stands now.
#!/usr/bin/perl
$file ="/usr/domains/output/Subscribe.txt";
if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@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;
$contents{$name} = $value;
}
}
if (-f $file)
{
#umask 002;
open(OUTPUT, ">>$file"
#chmod 0666, $file;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",10);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT "$contents{'field'}\n";
close (OUTPUT);
}
else
{
umask 002;
open(OUTPUT, ">>$file"
close (OUTPUT);
chmod 0666, $file;
#chown 318, , $file;
open(OUTPUT, ">>$file"
flock OUTPUT, 2;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",10);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT "$contents{'field'}\n";
flock OUTPUT, 8;
close (OUTPUT);
}
sub RPad {
local($str, $len, $chr) = @_;
$chr = " " unless (defined($chr));
return substr($str . ($chr x $len), 0, $len);
}
exit;
Any help would really be appreciated.
Thanks
RHNewfie There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
#!/usr/bin/perl
$file ="/usr/domains/output/Subscribe.txt";
if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@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;
$contents{$name} = $value;
}
}
if (-f $file)
{
#umask 002;
open(OUTPUT, ">>$file"
#chmod 0666, $file;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",10);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT "$contents{'field'}\n";
close (OUTPUT);
}
else
{
umask 002;
open(OUTPUT, ">>$file"
close (OUTPUT);
chmod 0666, $file;
#chown 318, , $file;
open(OUTPUT, ">>$file"
flock OUTPUT, 2;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",10);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT "$contents{'field'}\n";
flock OUTPUT, 8;
close (OUTPUT);
}
sub RPad {
local($str, $len, $chr) = @_;
$chr = " " unless (defined($chr));
return substr($str . ($chr x $len), 0, $len);
}
exit;
Any help would really be appreciated.
Thanks
RHNewfie There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try