Link to home
Start Free TrialLog in
Avatar of emma-louise
emma-louise

asked on

Redirect in CGI

I am using the following script to process my email form.  However, although it works ok, I don't want to display html thank you's that are inside the script.  I want to simply redirect to a thankyou.html page.  How can I accomplish this?

http://www.demon.co.uk/tools/formmail/form-mail.pl

Many Thanks
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of manav_mathur
manav_mathur

Tintin, one thing I've noticed is that although what you have said is repeatedly commented in other questions too,
CGI::redirect()

produces a header which contains a STATUS field too. Is that of significance??

Manav
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
print "Location: ..";

as first output should be enough as long as the server is not configured for non-parsing headers (NPH), which is the most common default
using CGI to redirect is overhead (but in this case probably ok, cause CGI is already loaded)
you can also do:

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

use CGI qw(:standard);

print redirect("www.google.com");

Interesting,

There was a discussion going on the other day on perlmonks about the usefulness of http:#13642419 over the other method. I specifically stated an example of what has been posted in http:#13642419 . I dont see formmail.pl using CGI anywhere, do we're better of with Tintin's suggestion. However, if you do want to use CGI,

#!/usr/bin/perl -w
use CGI qw/redirect/;
print redirect("www.google.com");

would probably save our namespace a bit. Doesnt count usually, but will matter definitely in case your site hit-rate is good.

Manav
   

 
   

However, if you insist on using formmail, this is the modified script

# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';

# This should be set to the username or alias that runs your
# WWW server.
$recipient = 'webmaster@your.site.here';

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # Stop people from using subshells to execute commands
    # Not a big deal when using sendmail, but very important
    # when using UCB mail (aka mailx).
    # $value =~ s/~!/ ~!/g;

    # Uncomment for debugging purposes
    # print "Setting $name to $value<P>";

    $FORM{$name} = $value;
}

# If the comments are blank, then give a "blank form" response
if ($FORM{'comments'}) {

# Now send mail to $recipient

open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL "Reply-to: $FORM{'username'} ($FORM{'realname'})\n";
print MAIL "Subject: WWW comments (Forms submission)\n\n";
print MAIL "$FORM{'username'} ($FORM{'realname'}) sent the following\n";
print MAIL "comment about The Tech's WWW server:\n\n";
print MAIL  "------------------------------------------------------------\n";
print MAIL "$FORM{'comments'}";
print MAIL "\n------------------------------------------------------------\n";
print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close (MAIL);
}

# Print out a redirection header. Change your site HERE
print 'Location: http://www.somesite.com/page.html\n\n';



Manav, your local ID 13642419 in http:#13643444 is not in this thread, can you please corect
I was referring to xDamox's post. Im still unclear on how you get these ids. Specifically, if you have joined in the thread late and want to know the id for an earlier question.

Manav
<off-topic>
these IDs are in the HTML source only as a A NAME=, only EE admins, PEs, mods see them in the browser
don't ask me why

Manav, was your reference to http:#13643403 ?
</off-topic>
Yes. My comment was regarding
1) the uselessness of importing all symbols(tagges standard) into the current namespace when only 'redirect' was used.
2) as an implication, why use CGI at all and not go with what Tintin had suggested in the http:#13618236 (hope I get it right this time). I can't see formmail.pl 'use'ing anywhere.

Manav
>....formmail.pl 'use'ing anywhere
....formail.pl 'use;ing CGI anywhere
at least to valid working suggestions (Tintin, amc25), no reason for a delete