Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy one: Premature end of script headers

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
OK, I've only been doing CGI for a few hours. To be safe I changed my sripts to match the ones on the forum's FAQs.

The error message "Premature end of script headers" keeps logging everytime I try & execute the following test.html script from the Apache (1.3.19) server. (Linux)

I can run the test.html script locally but it won't call the test.cgi locally. I think I know why. However I can run the test.cgi script on the server & it just tells me it doesn't like the email address.

Is this an Apache setting or something? Oh yea, the permissions for the programs are 755.

test.html =============================

<HTML>
<HEAD><TITLE>Most Basic CGI Input
Page</TITLE></HEAD>
<BODY>
<FORM ACTION=&quot;/cgi-bin/test.cgi&quot; METHOD=&quot;POST&quot;>
<P>email, please:
<INPUT TYPE=&quot;TEXT&quot; WIDTH=&quot;25&quot;
NAME=&quot;Email_Address&quot;>
</P></FORM></BODY></HTML>

test.cgi =============================

#!/usr/bin/perl
use CGI;

# instantiate a new CGI object. Call it $q.
$q = new CGI;
print $q->header;
print $q->start_html;
# retreive the Email_Address parameter submitted in the HTML
$email = $q->param('Email_Address');

# check to see if it looks like an email.
unless ($email =~ /.*@.*\.\w+/) { &killme(&quot;Not a valid email address&quot;); }
print &quot;<P>Email is $email</P>\n&quot;;
print $q->end_html;

sub killme {
print &quot;<P>$_[0]</P>&quot;;
print $q->end_html;
die;
}
 
OK, Got it...

Total newbie mistake!

Raw HTML is only being accepted in the /htdocs dir. Why I'll look into myself & won't bother you with here.

Silly, silly boy :-D
 
I'm confused. Does that mean the cgi program with the embedded raw html is placed in /htdocs? I thought all cgi programs go into cgi-bin. I'm having this problem with the first cgi program in a book that doesn't mention this problem.

Thank You,
Doug
 
Actually, I see that your cgi program is in /cgi-bin. But what goes in /htdocs? How did you solve the error?
Doug
 
Static html pages go in htdocs.

Stuff that you want apache to run(execute), like CGI apps, goes in your cgi-bin dir. The cgi code is segregated for security reasons. Your Apache configuration (httpd.conf file) identifies your cgi-bin (s). So, apache will only run (execute) files found in the your cgi-bin. (Those that are in htdocs are simply read and spit back to the browser, not executed.) That being the case, don't put anything in your cgi-bin that you don't want the world to be able to run. If a file is executable and in your cgi-bin, the world can ask apache to run the program. Depending on the program, that could be very dangerous. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
I'm still a little confused. Here is my cgi program. This is my program, as it is in the book. I have inserted
print &quot;Content-type:text/html\n\n&quot;; above &quot;use CGI&quot; and changed <<HTML; to <<EndofHTML and HTML to EndofHTML. Then I changed &quot;url&quot; in the declaration of $self to the url of this program. (The author doesn't say what put there.) And I'm still getting the &quot;Premature end of script headers&quot; error. The book is &quot;Perl Web Site Workshop&quot;, and this is the first program in the book. At this point I find it not very copious. Any other suggestions?
Thank You,
Doug

#!/usr/bin/perl -w

use CGI; # import the CGI.pm module

$q = new CGI; # create a new CGI object, $q,
# which will automatically read
# any input and format it for
# easy access.

$self = $q->url; # and get our own url

# start the page by printing a standard
# HTTP header (&quot;Content-type: text/html&quot;)
print $q->header;
# If we've gotten some input, display the result page.
# Otherwise, display the input form.
if ($q->param){

# Make the name look nice by capitalizing the
# first letter
$name = ucfirst( $q->param('name') );
# This is a 'here document': everything down to the
# label specified (in this case, 'HTML') will be
# printed. The closing HTML label must be the first
# word on its line.
print <<HTML;
<html>
<head>
<title>My First CGI</title>
</head>

<body>
<h1>My First CGI</h1>

<p>Hello, $name! This is my first CGI!</p>

</body>
</html>

HTML

} else {

# Another 'here document' -- note that
# it's not a good idea to have two with the
# same label in one script.
print <<FORM;
<html>
<head>
<title>What's your name?</title>
</head>

<body>
<h1>What is your name?</h1>

<form action=&quot;$self&quot; method=&quot;post&quot;>
<p>Your name: <input type=&quot;text&quot; name=&quot;name&quot;/></p>
<p><input type=&quot;submit&quot;/></p>
</form>

</body>
</html>

FORM


 
You are missing a closing '}' for your else block.

[red]
} else {
# code here
} # you need this closing bracket
[/red]

I fixed that one problem and the code works on my machine.
You can test for simple syntax problems like that from the command line.


There are a few other sneaky issues that might trip you. Let us know if the closing } does not make it work. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Actually, I neglected to copy that bracket when I pasted the code, so it has been there. Sorry. I think the problem might be the value I'm putting for &quot;url&quot; in the declaration of $self. I'm trying the url path of the cgi page itself, but I really don't know what to put in there. The only help the author gives on this is the comment, &quot;# and get our own url&quot;, and I don't have a clue of what that means.
The error message reads...

[Wed Aug 21 14:17:48 2002] [error] (8)Exec format error: exec of (program pathname) failed
[Wed Aug 21 14:17:48 2002] [error] [client 64.156.224.146] Premature end of script headers: (program pathname)

Does the (8) mean line eight? Because the line where $self is declared is the next line of uncommented code after line 8.

I have compiled it on the command line using perl -c first.cgi and perl -cw, and both result in no errors or warnings. As far as the <<HTML is concerned, all other cgi pgms in my /cgi-bin use <<EndofHTML, and most of them work. Could that have something to do with it? I've tried that and <<EndofForm, but it doesn't work.
Thanks!
Doug
 
I don't know what the (8) means.

If you can check your syntax from the command line and it
works, then there is nothing wrong with that code.

I don't see anything wrong with the way you are setting $self. 'url' is a method in the CGI.pm 'classs'. The line
$self = $q -> url;
runs the 'url' method on the $q object producing the fully qualified url for the $q object which is stored in $self.
You've used it appropriately.

Assumption:
1 - judging from your #! line, you're trying to run this on
a Unix or Linux box.

There are only a few issues to check:
1 - syntax - via perl -c we've establised that you're
OK

2 - line endings - If you are typing in or copying
from a win-PC, then you must convert the Win style line
endings to *nix style line endings. This can be done
using the dos2unix command on the *nix box or on-the-fly
with most popular ftp clients.

3 - The piece of code must be executable. This can be
done on the *nix box with the chmod command.
prompt% chmod +x first.cgi
Or, many ftp clients will let you specify the permissions
and they will be set upon transfer of the file to the
*nix box.

4 - If you are moving the code from a local box to a server
to run, make sure the #! line really points to the
perl interpreter on the server. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Good points. I always ensure three and four are taken care of. I have tried dos2unix filename.cgi newfilename.cgi in the unix system and on my shell client, but I get the &quot;ksh: dos2unix: not found.&quot; I did actually email the program from my pc to my unix account. And from the unix account, I ftp the program to the cgi-bin directory in my webhosting account. Btw, if I wasn't attending school at this time, I wouldn't have a unix account. So, I don't see how people get a unix account, does the webhost provide that somehow? Do I need to download Perl for the pc in order to use these cgi programs?

~Doug
 
&quot;I did actually email the program from my pc to my unix account. And from the unix account, I ftp the program
to the cgi-bin directory in my webhosting account.


If the text/code originated on a Win PC and you are
moving it to a *nix box, then you will have to find a way
to convert the line endings. Many text editors will do
that for you. Many GUI FTP clients will also. If you don't see an obvious opportunity, check with you web hosting company. This is a common issue and they may have a good way to handle this.



Btw, if I wasn't attending school at this time, I
wouldn't have a unix account. So, I don't see how
people get a unix account, does the webhost provide
that somehow? Do I need to download Perl for the pc
in order to use these cgi programs?


In order to run a piece of CGI, you need a program that
obeys the CGI rules (eg. first.cgi) and a web server ready
to server CGI applications (eg. apache).

You can work in any of a variety of situations.
1 - You can have Perl( and Apache
( on your PC, and do
all your CGI code development on your PC. If you
are running NT, 2000, or XP, you can use Apache to serve
your pages right off of your PC. Apache does not
recommend using their web server on Win9* boxes for
public web serving. If you don't have a static IP address
on your PC or are behind a firewall or other impediment, go
to situation 2.

2 - You can have Perl on your PC with or without a web server.
You can check the syntax with perl -c and then move (ftp)
the code up to your web host. There are a number of
free ftp clients for windows boxes. I have used WSFTP
for awhile and am happy with it. It will let you do
the convert line endings and set the execute bit on
completion of a file transfer.

3 - you can use a more circuitous route, like the one
you are currently using. If you have a web host, there
is no need to move your code from your PC to your student
unix account and then to the web host. You can go
straight from your PC to your web host. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
I am also a newbie at Perl and CGI. Some time ago, I noticed that I was lacking a lot of LINUX knowledge to work with Perl-Apache-CGIs. So I installed LINUX-Mandrake 8.1. I have learned a lot. But I am still learning Perl an CGI.
What your message said is that your server is working with KShell that is a limited BASH shell (a sort of highly improved DOS batch, and actually very similar).
There are some shell commands that you should know:

which perl #should answer the following after pressing the <Enter> key...
/usr/bin/perl
that says that your perl program is found at folders /usr/bin/. At dos you should just look for the .exe file and write the path for yourself: that is c:/Perl/perl (or whatever).

At *nix systems, there is also a security fact that is handled as MS DOS's write permissions:
Do these shell commands...

cd /var/ #if cgi-bins are at that place in your system...
ls -lF *.cgi #to see how are the other .cgi programs configured (the just copy those 'chmod' codes)
#see the column of numbers at the begining of the output?
#if the files start with 552 or 522 or 52 just copy this...
chmod 522 first.cgi #your chmod codes are setup,now!
ls -lF *.cgi #find your file, in the list, see it changed
#see the names after the numbers (may be... apache apache)?
#put the same 'chown' names for your file (owner and group)
chown apache.apache first.cgi
Hope this digested summary of *nix could help you. (may be you are not allowed to do these changes at your site, but you can do it if you where a user named 'root'. Ask your Webmaster.)
I am also working with Apache 2 at my Win ME and all works fine! There are no security troubles at Windows...
But you should change the starting line of your program to suite where perl.exe is installed.
Hope it helps (be patient).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top