?
Solved

Premature end of script headers / Internal Server Error

Posted on 2005-05-17
10
Medium Priority
?
238 Views
Last Modified: 2013-12-25
We have a site using Perl which began giving the Internal Server Error. It consists of two pages. The first is a login page called "login". The second is a ordering page called "order". I'm not too familiar with Perl so I'm hoping to get some info to clear up this error.

We first open the login page domain.com/cgi-bin/login and looking at the error logs it gives the error:
      Use of uninitialized value in concatenation (.) or string at login line 22.
Here is the code from that page with line numbers and comments/line spaces removed:
  1: #!/usr/bin/perl -w
  8: use CGI;
  9: $q = new CGI;
14: my $order_cgi = "http://domain.com/cgi-bin/order";
17: my $errstr = "<h2>Invalid username or password</h2>" if $q->param('ERROR');
20: print $q->header;
22: print <<"EOF"
23: <html>
.... etc, etc..

On the login page we enter a username/password, click enter and the ordering page is loaded which is at domain.com/cgi-bin/order. This page doesn't load but instead comes up with the classic Internal Server Error. looking at the error logs it shows:
[Tue May 17 12:22:41 2005] [error] [client my.ip.here] Premature end of script headers: /home/httpd/vhosts/domain.com/cgi-bin/order
Here is the first part of the code up to the end of the headers:
<%args>
$Username => ""
</%args>
<%init>
use DBI;
my $dbh = DBI->connect("DBI:CSV:f_dir=/home/rhaynes/db")
 || die "Cannot connect: " . $DBI::errstr;
 my $sth = $dbh->prepare("select * from robin where Username=?");
 my $nr=$sth->execute($Username);
 my @row = $sth->fetchrow_array();
</%init>
<html>
<head>
<script language="JavaScript">
<!--
a whole bunch of variable initialized with:
var
and some functions
//-->
</script>
<title>Home Page</title>
</head>

What would be causing these errors?
0
Comment
Question by:wookaka
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 13

Assisted Solution

by:StormyWaters
StormyWaters earned 400 total points
ID: 14022107
If I recall correctly, and mind you I'm not a CGI programmer, there aren't any quotes around "EOF" when using it the way you have on line 22. I don't know if that's the only error. It should be:

print << EOF; #semicolon here.
<html>
...etc...
EOF #not here.
0
 

Author Comment

by:wookaka
ID: 14022633
Unfortunately when removing the quotes the login page now gives Internal Server Error and doesn't load at all.
The semicolon doesn't seem to make a difference.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 400 total points
ID: 14023112
The easiest way to see the perl errors is by adding:

use CGI::Carp qw(fatalsToBrowser);

to your script.

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:wookaka
ID: 14023152
I tried:

use CGI::Carp qw(fatalsToBrowser);
use CGI;
$q = new CGI;

and

use CGI::Carp qw(fatalsToBrowser);
$q = new CGI;

...the first doesn't change anyting and the second gives an error saying that CGI wasn't initialized.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 14024321
The 2nd one won't work as you are trying to initialise a CGI object without using the CGI module.

Try adding:

$|=1;

near the top of the script to turn off buffering.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 14025389
please start the script from command line and post result verbatim
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 800 total points
ID: 14030265
My first assumtion is that the database connection is failing and the die statement is being executed/printed prior to the headers.

>> The semicolon doesn't seem to make a difference.
It does make a difference.  If you send warnings to the browser in addition to the fatal errors, you'll see the error that's being generated when you don't have the semicolon.


use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

warningsToBrowser(1);   # put this after printing the headers
0
 

Author Comment

by:wookaka
ID: 14032017
I'm not sure what command you would like me to use to "start the script from command line and post result verbatim"??? I used bash and got the following:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
: command not founddomain.com/cgi-bin/order:
/home/httpd/vhosts/domain.com/cgi-bin/order: use: command not found
: command not founddomain.com/cgi-bin/order:
/home/httpd/vhosts/domain.com/cgi-bin/order: line 24: syntax error near unexpected token `qw(f'
'home/httpd/vhosts/domain.com/cgi-bin/order: line 24: `use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

After looking at the database it was loading it looks like that was a problem. The customer had uploaded an old page to try to get things working but did not fix the problem. I put the correct page up so the database looks like it should be loading correct.  In the general error logs we're still getting the premature end of script headers. I've included the top portion of the corrected orders page below. Spaces and comments removed to keep things short.
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);      ### added to show errors in browser
$q = new CGI;
$db_file = "/home/httpd/vhosts/domain.com/db/robin.csv";
$login_url = "http://domain.com/cgi-bin/login";
$dberror_url = "http://domain.com/cgi-bin/dberror";
$user = $q->param('Username');
$pass = $q->param('Password');
unless ($user && $pass)
{
  print $q->redirect($login_url . "?ERROR=1");
  exit;
}
unless (open(DB, $db_file))
{
  print $q->redirect($dberror_url);
  exit;
}
<DB>; # Get rid of the header line...
while (<DB>)
{
   @row = split /,/;
   last if $row[0] eq "$user";
}
close DB;
if ($row[0] ne "$user" || $row[1] ne "$pass")
{
   print $q->redirect($login_url . "?ERROR=1");
   exit;
}
print $q->header;
print <<"EOF";       ## added the semicolon
<html>
<head>
etc, etc,

I've tried putting warningsToBrowser(1); in several locations but none actually display the errors on the browser. Where excatly should this go? after the last EOF? I apologise I'm not at all familiar with Perl so detailed instructions on exactly where code should go is much appreciated.

Thanks
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 14033147
print $q->header;
warningsToBrowser(1);
print <<"EOF";
<html>   #  remove this line because it's already printed via print $q->header;
<head>  #  remove this line also
etc, etc,

It may not make a difference but I don't think you need the exit statement after the redirect.
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 400 total points
ID: 14037515
>  I'm not sure what command you would like me to use ..
the command is the script you posted
In bash simply do:
   perl your-script-name here
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question