PDA

View Full Version : CGI - Firefox - source rendered not HTML.


jeff_porter
04-03-2006, 04:49 PM
Hi all,

this is my first post, to be gentle :confused:

I hava a cgi script to submit a page (email form).

The script is runs & the email is sent, but the HTML at the end to say its sent doesn't get rendered in FireFox.

Can anyone help me?

Thanks
Jeff


Script...

#!/usr/bin/perl
require "subparseform.lib";
&Parse_Form;
use CGI;
$sendto = 'me@home.com';
$mailprogram = "/usr/lib/sendmail";
$firstname = $formdata{'firstname'};

open (MAIL,"|$mailprogram -t");
print MAIL "To: $sendto\n";
print MAIL "From: <mailform\@awcweb.com>\n";
close ( MAIL );

print <<END_HTML;


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div>Thank you</div>
</body>
</html>

bazz
04-03-2006, 05:11 PM
Hi,

I ain't a pro either but, if that is the full code for your page, then I would suggest you add,to the top of the html, a DOCTYPE. Then you might need to add the content-type line.

And then don't forget to close your print <<END_HTML; with END_HTML
(All suggestions shown in red).

#!/usr/bin/perl
require "subparseform.lib";
&Parse_Form;
use CGI;
$sendto = 'me@home.com';
$mailprogram = "/usr/lib/sendmail";
$firstname = $formdata{'firstname'};

open (MAIL,"|$mailprogram -t");
print MAIL "To: $sendto\n";
print MAIL "From: <mailform\@awcweb.com>\n";
close ( MAIL );


print "Content-type: text/html\n\n";

print <<END_HTML;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="EN-GB">

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div>Thank you</div>
</body>
</html>
END_HTML


bazz

jeff_porter
04-03-2006, 05:21 PM
Thanks for the reply Bazz.

I've tried what you suggested, but it just results in a "Internal Server Error - 500".

I suspect you're right, that it requires something like..

print "Content-type: text/html\n\n";

JP

More info the the file...

[Mon Apr 3 16:17:58 2006] [error] [client 137.205.177.198] Premature end of script headers: /home/public_hmtl/contact.cgi

Anyone got any ideas?

FishMonger
04-03-2006, 06:09 PM
You specifically say that the page doesn't render in firefox, but does it render in any other browser? Do you have access to the server error log, and if so, what error messages does it have.

If it's not too long, can you post the complete script?

jeff_porter
04-03-2006, 06:14 PM
The code works fine for IE.

Here is the complete script...

#!/usr/bin/perl

require "subparseform.lib";
&Parse_Form;

use CGI;

$sendto = 'xxxxxxxx.xxxxx@gmail.com';
$mailprogram = "/usr/lib/sendmail";

$firstname = $formdata{'firstname'};
$surname = $formdata{'surname'};
$company = $formdata{'company'};

# sending mail info
open (MAIL,"|$mailprogram -t");
print MAIL "To: $sendto\n";
print MAIL "From: <mailform\@xxxxxx.com>\n";
print MAIL "Subject: Registration Info From Conference 2006 Website\n\n";
#personal info
print MAIL "Name: $firstname $surname\n";
print MAIL "From: $company\n";

close ( MAIL );




print <<END_HTML;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="EN-GB">
<head>
<title>Conference</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/thecss.css" rel="stylesheet" type="text/css">
</head>

<body>
<div class="mainbodyleft" id="maintitles">Thank you</div>
</body>
</html>


END_HTML

EndBlock





[Mon Apr 3 16:21:55 2006] [error] [client 137.205.177.198] Premature end of script headers: /home/public_html/conference/contact.cgi


Thanks for helping me out with this.

mlseim
04-03-2006, 10:44 PM
Try this ... see if it displays "Your text"
Put it right after "close (mail)" and eliminate the current HTML stuff.
Then, don't use any quotes in your HTML (use single quotes)

print "Content-type: text/html\n\n";
print "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title> Conference </title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
<link rel='stylesheet' href='css/thecss.css' id='folio' type='text/css' media='all' />
</head>
<body>

Your text

</body>
</html>

";

FishMonger
04-03-2006, 11:35 PM
#!/usr/bin/perl

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

my $cgi = new CGI;
my %form = $cgi->Vars;
my $sendto = 'xxxxxxxx.xxxxx@gmail.com';
my $mailprogram = "/usr/lib/sendmail";

print $cgi->header;
warningsToBrowser(1);
print $cgi->start_html(-title => 'Conference', -style => {-src => 'css/thecss.css'});

# sending mail info
open (MAIL,"|$mailprogram -t") || die "can't pipe to sendmail $!";
print MAIL "To: $sendto\n";
print MAIL "From: <mailform\@xxxxxx.com>\n";
print MAIL "Subject: Registration Info From Conference 2006 Website\n\n";
#personal info
print MAIL "Name: $form{'firstname'} $form{'surname'}\n";
print MAIL "From: $form{'company'}\n";

close ( MAIL );

print '<div class="mainbodyleft" id="maintitles">Thank you</div>', $cgi->end_html;

Edit:
I enabled the redirection of wanings to the browser, but I forgot to enable the warnings. Add this line near the top.

use warnings;

jeff_porter
04-04-2006, 09:54 AM
Again, thanks to you all for the help you've given me on this.


FishMonger:

I've tried you code (copy/paste) and instead of getting the souce of the page displayed, I get the web page "internal server error 500".

Log file shows...

[Tue Apr 4 08:48:57 2006] [error] [client 137.205.177.198] Premature end of script headers: /home/public_html/conference/contactx.cgi

mlseim:

I've tried your code also & still see the HTML source, and again the log file shows...
[Tue Apr 4 08:49:12 2006] [error] [client 137.205.177.198] Premature end of script headers: /home/public_html/conference/contactx.cgi

I know the most common reason for the "premature end of script headers" is due to not defining the content type...
print "Content-type: text/html\n\n";

mlseim
04-04-2006, 01:46 PM
But again ... the script I showed you works with IE, but not with Firefox?

... and you don't have that "EndBlock" thing on there anymore, right?

jeff_porter
04-04-2006, 02:25 PM
THANK YOU!!!

I'm not sure what I've done. But I deleted the cgi file from the server,
created it again from scratch. New text file & copy in code.

Uploaded the file, set the permissions & bingo!! it works!

I'm not going to question it this time. :)

Thanks again for all your input. I owe you lot a beer!

Jeff

FishMonger
04-04-2006, 03:22 PM
Jeff,

It's unclear which version you ended up using, but I'd like to point out a couple things.

Your script is loading the CGI module, which is good, but you never use it. Instead you use:

require "subparseform.lib";
&Parse_Form;

That is almost assuredly using the outdated method of parsing the form submission. If it looks anything like this, it should be dropped.
sub parse_form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

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

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

# First, we must translate the form encoding back to ascii:
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

if ($FORM{$name} && ($value)) {
$FORM{$name} = "$FORM{$name}, $value";
}
elsif ($value) {
$FORM{$name} = $value;
}
}
}

The CGI module does a number of checks/validations that this older method leaves out. The CGI module is the Perl de faqto standard for handling the form parsing, as well as other areas of the cgi output.

Instead of using a here document to hand roll and output the entire html code, take advantage of the modules methods. Using the cgi methods I showed, you can reduce your "thank you" page down to 1 or 2 simple print statements and be assured that the headers are standards compliant.

Here's a single print statement:
print $cgi->header,
$cgi->start_html(-title => 'Conference', -style => {-src => 'css/thecss.css'}),
'<div class="mainbodyleft" id="maintitles">Thank you</div>', $cgi->end_html;