19.13. Saving a Form to a File or Mail PipeProblemYour CGI script needs to save or mail the entire form contents to a file. Solution
To store a form, use the CGI module's # first open and exclusively lock the file open(FH, ">>/tmp/formlog") or die "can't append to formlog: $!"; flock(FH, 2) or die "can't flock formlog: $!"; # either using the procedural interface use CGI qw(:standard); save_parameters(*FH); # with CGI::save # or using the object interface use CGI; $query = CGI->new(); $query->save(*FH); close(FH) or die "can't close formlog: $!"; Or, save to a pipe, such as one connected to a mailer process: use CGI qw(:standard); open(MAIL, "|/usr/lib/sendmail -oi -t") or die "can't fork sendmail: $!"; print MAIL <<EOF; From: $0 (your cgi script) To: hisname\@hishost.com Subject: mailed form submission EOF save_parameters(*MAIL); close(MAIL) or die "can't close sendmail: $!"; Discussion
Sometimes all you want to do with form data is to save it for later use. The
File entries are stored one per line as
If you want to add extra information to your query before you save it, the param("_timestamp", scalar localtime); param("_environs", %ENV); Once you've got the forms in a file, process them by using the object interface.
To load a query object from a filehandle, call the use CGI; open(FORMS, "< /tmp/formlog") or die "can't read formlog: $!"; flock(FORMS, 1) or die "can't lock formlog: $!"; while ($query = CGI->new(*FORMS)) { last unless $query->param(); # means end of file %his_env = $query->param('_environs'); $count += $query->param('items requested') unless $his_env{REMOTE_HOST} =~ /(^|\.)perl\.com$/ } print "Total orders: $count\n"; File ownership and access permissions are an issue here, as they are in any files created by CGI script. Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|