+ Reply to Thread
Results 1 to 5 of 5

Thread: Perl/CGI: Help writing form data to a file

  1. #1
    danni.bayn16 is offline x10Hosting Member danni.bayn16 is an unknown quantity at this point
    Join Date
    Aug 2010
    Posts
    2

    Perl/CGI: Help writing form data to a file

    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
    }

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Perl/CGI: Help writing form data to a file

    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 by descalzo; 08-26-2010 at 03:27 PM.
    Nothing is always absolutely so.

  3. #3
    danni.bayn16 is offline x10Hosting Member danni.bayn16 is an unknown quantity at this point
    Join Date
    Aug 2010
    Posts
    2

    Re: Perl/CGI: Help writing form data to a file

    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!

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Perl/CGI: Help writing form data to a file

    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.
    Nothing is always absolutely so.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Perl/CGI: Help writing form data to a file

    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 by misson; 08-26-2010 at 04:56 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Line break while writing txt file using php & form data
    By vaibhavy in forum Programming Help
    Replies: 3
    Last Post: 03-31-2010, 11:28 AM
  2. PHP writing to a file outside a directory
    By NightFox in forum Programming Help
    Replies: 3
    Last Post: 01-23-2009, 05:22 PM
  3. Replies: 6
    Last Post: 08-25-2008, 09:43 AM
  4. Created and writing form data a to CSV file
    By HeavyMetalMan in forum Free Hosting
    Replies: 0
    Last Post: 12-05-2007, 02:55 PM
  5. Problem writing to a file (php)
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 06-06-2006, 02:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers