Forum Moderators: coopster & phranque

Message Too Old, No Replies

Browser trying to download CGI script after form is sent.

browser download CGI script problem

         

pugg09

7:56 pm on Nov 30, 2008 (gmt 0)

10+ Year Member



Doing a search didn't help.....sorry to bug you.

I have a problem, everytime I send my form to the PERL script - the browsers tries to download my CGI file. It still process the form data - but does anyone know why my browser would do this.

Here's the code

#!/usr/bin/perl
# NOTE- on above line- your path to PERL could be different!
# If you're not sure, check with your web host.
###########################################################
# accepts data post from 'contact us' page and emails data
# written 04/28/02 by thomas watson
###########################################################
# NOTE- again, your path to sendmail could be different
$mailprog = "/usr/sbin/sendmail";
# NOTE- this is where you want the email sent to.
# Be sure and "escape" the "@" sign.
$recipient = "myemail\@example.com";
# get the input
# put it in $buffer
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# split the input into name-value pairs
@pairs = split(/&/, $buffer);
# standard processing loop
# this will un-URLencode as many variables as you send and place
# in an array ($FORM).
foreach $pair (@pairs) {
# split the name-value pair into name and value
($name, $value) = split(/=/, $pair);
# convert '+' to space
$value =~ tr/\+/ /;
# convert embedded comma to space delimiting purposes
$value =~ tr/\,/ /;
# convert hex symbols to alphanumeric
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("c", hex($1))/ge;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("c", hex($1))/ge;
$value =~ s/<!--(.¦\n)*-->//g;
# create new array with each name and value as elements
$FORM{$name} = $value;
}
# now assign a variable name to each value from the input array
$NAME = $FORM{'name'};
$EMAIL = $FORM{'email'};
$SUBJECT = "Request For Quote";
$COMPANY = $FORM{'company'};
$PHONE = $FORM{'phone'};
$MESSAGE = $FORM{'message'};
# next we have to re-URLencode any values being sent back to flash.
# for my contact form I'm only going to send the name.
$NAME =~ tr/ /\+ /;
# this assigns a "name" to our name/value pair being sent
# and sends it back to flash through the standard output stream.
# be sure and note the content type!
print "Content-type: application/x-www-urlform-encoded\n\n";
print "rtn_name=$NAME";
# now we open a stream to "sendmail" and send the email
# using the variables from above.
open (MAIL, "¦$mailprog $recipient") ¦¦ die "Can't open $mailprog!\n";
print MAIL "To: $recipient\n";
print MAIL "From:$EMAIL\n";
print MAIL "Subject: $SUBJECT\n";
print MAIL "------------------------------------------------------\n";
print MAIL "$MESSAGE\n\n";
print MAIL "$NAME\n";
print MAIL "$COMPANY\n";
print MAIL "$PHONE";
close (MAIL);
exit;

[edited by: eelixduppy at 9:24 pm (utc) on Dec. 1, 2008]
[edit reason] specifics [/edit]

rocknbil

3:33 pm on Dec 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but does anyone know why my browser would do this.

This is almost always due to the script printing something to the browser before a content-type header has been sent, or in your case, an unrecognized content-type header.

Looking at your (highly insecure) script, note that the only header is printed prior to sending mail:

print "Content-type: application/x-www-urlform-encoded\n\n";

This is a valid header, but only for sending data to a server, not reading it in a browser.

I'm not sure how this "Gets sent back to flash" but if this content type header is sent to the browser, it will most surely prompt a download, which is what's happening.

Perhaps you're not implementing it properly, it looks to me like this script should be called from within a flash object via getURL() or related external data methods. If you request this script from a browser it will indeed prompt a download.