Tom O'Brien wrote:
: Can someone please give me an example of how to pass form data between
: two CGI programs? Here's what I'm attempting to do.
:   I'm a novice perl programmer at best so I'm just trying to modify two
: programs to do what I need. Basically, I'm trying to use a "affiliate"
: program. What should happen is that a form is submitted to
: "affiliate.pl" . Affiliate.pl then updates a counter and a few other
: things based on the form data passed to it. Affiliate.pl then passes the
: 
: form data over to my form handling script ("formhander.pl").  The
: problem is that some form fields get truncated when the data is passed
: back to my formhandler.
:   My basic understanding of things is that there are differences in the
: way "GET" and "POST" data are handled.

In a GET, the form data is attached to the URL, something like this:

http://ahost.com/cgi-bin/coolscript.pl?foo=bar&baz=stuff%20and%20nonsense

The problem with a GET is that if the query string (the thing after
"?") is big, it might be truncated to fit into a buffer somewhere.

POST data is sent in the body of the HTTP request, so it suffers no
such limitation.

Now I'm not sure what you mean by "passing form data between two CGI
programs".  Do you mean that affiliate.pl should act as a web agent and
hits formhandler.pl through a web access (in which case they could live
on different machines)?  or do you want affiliates.pl to execute
formhandler.pl from the command line?

In the former case, affiliate.pl should use the LWP modules to
create a little user agent and submit the CGI data with a POST request
to formhandler.pl. Then affiliate.pl will need to handle the output
returned from formhandler.pl.

In the latter case, which is what I'm betting you're doing, one way
might be to have affiliate.pl write the CGI data to a temporary file,
redirect the browser to formhandler.pl, and then have formhandler.pl
get the information from the temp file. CGI.pm makes this very easy
(you are using CGI.pm, right? ;):

affiliate.pl
------------
my $cgi = new CGI; # Handles GET or POST
...
my $tmpId = createId(); # Come up with a good temporary ID string
my $tmpFileName = createTempFileName($tmpId); # use $tmpId to create a temporary 
filename
open TMPFILE, ">$tmpFileName" or die "Can't open $tmpFileName: $!";
$cgi->save(*TMPFILE);
close TMPFILE;
print STDOUT $cgi->redirect("http://yourhost.com/cgi-bin/formhandler.pl?id=$tempId";);
        # Note: Redirects are always GETs
exit;

formhandler.pl
--------------
my $cgi = new CGI;
my $tmpId = $cgi->param("id");
my $tmpFileName = createTempFileName($tmpId); # Reconstruct the temp filename
open TMPFILE, $tmpfile or die "Can't open $tmpFileName: $!";
my $savedCgi = CGI->new(*TMPFILE);
close TMPFILE;
...

Then formhandler will have the saved CGI information in $savedCgi.

(Taint-check the daylights out of the $id, to prevent someone from
giving an ID like "../../../../etc/passwd". Also, the ID needs to be
unique for a given access. And don't forget to clean up the temp file
when you're done.)

Yes, it seems weird. but I do this in some scripts, where it works quite
well.  (It's especially nice because my scripts want to send you your
choice of a .tar file or a .zip file, and in the redirect, I can edit
the location to be something like
"http://yourhost.com/cgi-bin/formhandler.pl/stuff.$packageType?id=$tempId";,
where $packageType is one of "tar", "tar.gz", or "zip".  Then in
formhandler.pl, the package type will be available as $ENV{PATH_INFO},
and the default name that pops up in the user's save-as box will be
stuff.tar, stuff.tar.gz, or stuff.zip- whichever they selected- instead
of "formhandler.pl". A nice little convenience for the user.)

-- tdk

Reply via email to