Perl/CGI: Help writing form data to a file

danni.bayn16

New Member
Messages
2
Reaction score
0
Points
0
Hi all,
I have a perl/CGI script that I'm trying to use to write form data to a file. The script and the form seem to be working fine (confirmation and error pages are being displayed correctly), other than the data_file is not being created/written to.

This line:
open OUT, ">/home/feam/Data/data_hold.txt" || die "Can't open for writing: $!";
isn't working.
I've tried changing the path to the tmp and cgi-folder and have tried variations of relative rather than full path names. In each case, the confirmation page is shown correctly and all of my error and access logs are blank.

This script worked fine when I was doing my own hosting, but I am unfamiliar with how 3rd party hosting handles file permissions, scripting, etc.

Any help would be greatly appreciated!
Thanks,
danni

---------------------------------------
#!/usr/bin/perl -w

use Fcntl qw:)flock :seek);

my $time = localtime;
my $remote_id = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR};
my @name_value_pairs = split /&/, $ENV{QUERY_STRING};
my $NumRange = 10000;
my $RanNum = int(rand($NumRange));


if ($ENV{REQUEST_METHOD} eq 'POST') {
my $query = "";
read(STDIN, $query, $ENV{CONTENT_LENGTH} ) == $ENV{CONTENT_LENGTH};
push @name_value_pairs, split /&/, $query;
}



foreach $pair(@name_value_pairs) {
($name, $value) = split(/=/, $pair);
$FORM{$name} = $value;
}

if (!$FORM{'name'}) {
print "Content-type: text/html\n\n";
print "<head><title>Error: MISSING NAME</title></head>";
print "<body><p>You must enter a name to submit this form</p>";
print "<p>Please use the Back button to enter your name and resubmit</p>";
print "</body>";
print "</html>";
}
else {
open OUT, ">/home/feam/Data/data_hold.txt" || die "Can't open for writing: $!";
flock(OUT, LOCK_EX);
seek(OUT, 0, 2);
print OUT "hold\n";

foreach(@name_value_pairs) {
print OUT "$_\n";
}


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

print <<END_OF_PAGE;
<html>
<head><title>Thank you!</title></head>
<body>Thank you for completing your alignment judgements. </p>
</body>
</html>
END_OF_PAGE
}
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Code:
open( OUT, ">/home/feam/Data/data_hold.txt" )  || die "Can't open for writing: $!";

Perl is forgiving, but always use parentheses.

That should at least get your error messages.

Also, put

Code:
use CGI::Carp qw(fatalsToBrowser);

at the top of any script you are developing. It helps.
 
Last edited:

danni.bayn16

New Member
Messages
2
Reaction score
0
Points
0
More info: Added the parentheses and use CGI::Carp qw(fatalsToBrowser);
BTW: Thank you for the CGI line! yea! Descriptive error msgs!

It is a file permissions error. If I setup the data folder so that it is 777 the file is created perfectly.. unfortunately, this doesn't strike me as the most secure option.

Suggestions for how to fix the permissions? or should I just deal with the 777?

Thanks!
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Well..............

The problem seems to be that when Apache runs, it runs as nobody and the Perl scripts do not change uid to the script owner, but run as nobody.
If I remember correctly (have a problem with FTP at my current location), public_html has group 99 (nobody).
If you can somehow change the group id of the directoy/file to 99, you should be able to get by with just group writeable, not world writeable.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
PHP scripts should run under your account. You can write a one-off script to change the group using chgrp. While you're at it, use chmod to set the set-group-ID flag on Data, which will, by default, give all newly created files in Data the same group as Data; mode 02770 should do it.
 
Last edited:
Top