Premature end of script headers: mail.cgi

Y

yusuf

Hi,

I have script that does a database lookup, constructs a string, emails
it and then prints it out to the browser. The lines that actually print
to the browser are the last lines in the script:




$|=1;



print "Content-type: text/html\n\n";
print "SQL:<br>$statement<br><br>";



print "Sent email:<br><br>$msgbody";
print "\n\n";


Can someone please tell me why I get a lot of the "premature end of
script" errors?

Thanks.
 
U

usenet

yusuf said:
Can someone please tell me why I get a lot of the "premature end of
script" errors?

What happens when you run your script from a shell?

What happens if you add this near the top:

use CGI::Carp qw{ fatalsToBrowser carpout warningsToBrowser };
 
Y

yusuf

What happens if you add this near the top:

use CGI::Carp qw{ fatalsToBrowser carpout warningsToBrowser };

Nothing happens.

I also see the following errors when I get the premature end of script
headers error:

Use of uninitialized value in concatenation (.) or string at ...

The line the above error is referring to is the "$a = $a .
"\t<td>".uri_unescape($row[$i])."</td>\n";" in the below code:

while(my @row = $sth->fetchrow_array) {
$a = $a . "<tr>\n";
for(my $i=0; $i<12; $i++) {
$a = $a . "\t<td>".uri_unescape($row[$i])."</td>\n";



}
$a = $a . "\n</tr>\n";
}
 
P

Paul Lalli

yusuf said:
I have script that does a database lookup, constructs a string, emails
it and then prints it out to the browser. The lines that actually print
to the browser are the last lines in the script:

$|=1;

print "Content-type: text/html\n\n";
print "SQL:<br>$statement<br><br>";

print "Sent email:<br><br>$msgbody";
print "\n\n";

Can someone please tell me why I get a lot of the "premature end of
script" errors?

Possibly, but only if you give us all the relevant information. What
is *above* these "last lines of the script"? Do you have any other
prints there? Do you have anything that would generate errors or
warnings that would be sent to STDOUT?

This is why the Posting Guidelines for this group (which I'm pretty
sure you've been referred to already) ask you to please create a
*short* but *complete* script that demonstrates your error.

Paul Lalli
 
Y

yusuf

Paul said:
Possibly, but only if you give us all the relevant information. What
is *above* these "last lines of the script"? Do you have any other
prints there? Do you have anything that would generate errors or
warnings that would be sent to STDOUT?

There are no print statements above the ones given. Unfortunately, the
script that this is in have a lot of DB lookup code, so its kind of
long.
 
P

Paul Lalli

yusuf wrote:

Please do not snip attributions. I've put it back in below.
Nothing happens.

"Nothing" happens? As in, time suddenly stops? That seems unlikely.
*WHAT HAPPENS*? Do you get the same errors? Do you get no errors but
also no output? Does the program suddenly refuse to run at all? What?
I also see the following errors when I get the premature end of script
headers error:

What made you think that wasn't relevant the first time around? You
seem to almost be deliberately making it harder to help you.
Use of uninitialized value in concatenation (.) or string at ...

That is not an error. That is a warning. The distinction is
important.
The line the above error is referring to is the "$a = $a .
"\t<td>".uri_unescape($row[$i])."</td>\n";" in the below code:

while(my @row = $sth->fetchrow_array) {
$a = $a . "<tr>\n";
for(my $i=0; $i<12; $i++) {
$a = $a . "\t<td>".uri_unescape($row[$i])."</td>\n";

How many of these warnings did you get? It seems likely that at least
one of the $row[$i] values are undefined. If you got 12 of these
errors per database row, I'd say your fetch isn't returning any
results. If you got less than 12, I'd say you miscounted the number of
columns in your query. Again, without a short but *complete* script,
it's not possible to know.
}
$a = $a . "\n</tr>\n";
}


Paul Lalli
 
P

Paul Lalli

Paul Lalli also wrote, but yusuf snipped:
There are no print statements above the ones given. Unfortunately, the
script that this is in have a lot of DB lookup code, so its kind of
long.

*No one* asked you to post your entire script. Much the opposite. I
asked you to post a *short* but *complete* script that demonstrates
your error. Pare your problem down, removing all the code that's not
relevant to the error until you get the shortest possible complete
script that still generates the error. This is an imporant part of
debugging, and something you should be doing whenever you can't figure
out why your program doesn't work.

Paul Lalli
 
Y

yusuf

Heres a script that displays the behaviour. If you take out the loop,
it works:

#!/usr/bin/perl



use CGI;
use DBI;
use Net::SMTP;
use URI::Escape;
use strict;
use warnings;



my $head="";
my $tail ="";






for (my $i=0; i<100000; i++){



}









$head = <<"ENDA";



<html>
<head>
<title>Search QA Stats Server</title>
</head>
Test results:
<P>



ENDA









my $a = "";






$tail = <<"ENDB";
</table>
<P>
<hr noshade>
<P>
</body>
</html>
ENDB



my $msgbody = $head.$a.$tail;






$|=1;



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



print "Sent email:<br><br>$msgbody";
print "\n\n";
 
Y

yusuf

Sorry, the script below has other errors (in the for loop), please
ignore the posting.
 
U

usenet

yusuf said:
Sorry, the script below has other errors (in the for loop), please
ignore the posting.

Indeed it does (as Tad has also pointed out).

FWIW, the mesage you are seeing ("premature end...") is
characteristicly displayed in a browser when a CGI program is invoked
but the program fails because of a syntax or runtime error. The script
generates error messages (or maybe nothing at all), but the browser is
expecting a proper HTML page with (at least) a proper HTML header.
When the CGI program exits without providing the browser some valid
HTML, the browser complains.

The CGI::Carp statement I furnished _SHOULD_ have shown you the error
messages in the browser (ie, the same error messages you would see in a
shell). You say that "nothing" happens, which (as Paul points out) is
a rather inadequate description.

Your question focused on the part of your script which produces output.
But your script never got that far.
 
B

Ben Morrow

Quoth (e-mail address removed):
Indeed it does (as Tad has also pointed out).

FWIW, the mesage you are seeing ("premature end...") is
characteristicly displayed in a browser when a CGI program is invoked
but the program fails because of a syntax or runtime error. The script
generates error messages (or maybe nothing at all), but the browser is
expecting a proper HTML page

Not necessarily.
with (at least) a proper HTML header.

CGI header.
When the CGI program exits without providing the browser some valid
HTML, the browser complains.

The server complains. It generates an error page which the browser
displays perfectly normally.

Ben
 
S

Sherm Pendley

Paul Lalli said:
Pare your problem down, removing all the code that's not
relevant to the error until you get the shortest possible complete
script that still generates the error. This is an imporant part of
debugging, and something you should be doing whenever you can't figure
out why your program doesn't work.

It's also worth pointing out that at least nine times out of ten, this
process will lead you to finding your own solution without having to post
a question here at all.

sherm--
 
S

Sherm Pendley

FWIW, the mesage you are seeing ("premature end...") is
characteristicly displayed in a browser when a CGI program is invoked
but the program fails because of a syntax or runtime error. The script
generates error messages (or maybe nothing at all), but the browser is
expecting a proper HTML page with (at least) a proper HTML header.

That's completely wrong.

The "Premature end..." error is generated by the *server*, not the browser,
and it's generated when a script exits without creating a valid set of HTTP
(not HTML) headers.
When the CGI program exits without providing the browser some valid
HTML, the browser complains.

The browser is not complaining at all - all it's doing is displaying the
complaint that the server returned. Don't shoot the messenger. :)

sherm--
 
B

Ben Morrow

Quoth Sherm Pendley said:
(e-mail address removed) writes:

The "Premature end..." error is generated by the *server*, not the browser,
and it's generated when a script exits without creating a valid set of HTTP
(not HTML) headers.

Pick pick...

CGI (not HTTP) headers. They differ in some important respects (notably,
ending with "\n" rather than "\cM\cJ").

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top