1

I would like to know how to debug a cgi script that doesn't output any HTML.

I currently have a login page with a html form that upon submission redirects to another cgi script. This script is supposed to check whether the user has inputted correct information.

This script does not print out any html so I'm not sure how I am supposed to check it. I am currently receiving an Internal Server Error when the redirection occurs to this script. The error.log states:

End of script output before headers:

Could someone please tell me a way I can debug this script?

Thank you for your help.

main.cgi

#!/usr/bin/perl -w

use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use CGI::Cookie;
use Data::Dumper;  
use List::Util qw/min max/;
warningsToBrowser(1);

# print start of HTML ASAP to assist debugging if there is an error in the script
print page_header();

# Get Cookies
%cookies = CGI::Cookie->fetch;

# If no cookie, display login page
if (!%cookies){
   # Redirects the user to login.cgi
   print show_login_form();
}

#if cookie
#  do something

print page_trailer();

login.cgi

use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use CGI::Cookie;
use Data::Dumper;  
use List::Util qw/min max/;
warningsToBrowser(1);

$cookie_value = "";



# Convert request method to Uppercase eg: GET, POST
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

# If "POST" request sent, check login credentials
if ("$ENV{'REQUEST_METHOD'}" eq "POST"){
   # Get login parameters
   $username = param('username');   
   $password = param('password');   

   $loginCheckResult = check_login($username, $password);

   # If login was successful, create a cookie
   if ($loginCheckResult){
       # Set Cookie
       $cookie = CGI::Cookie->new(-name=>$cookie_value,-value=>$cookie_value);      


   # If login was Unsuccessful, redisplay the login page
   } else {
    # Do something here...  
   }
}

2 Answers2

0

The error End of script output before headers: means your script finished without outputting anything. Essentially, you've created a CGI::Cookie but not sent it to the browser:

$cookie->bake;

If you wanted to set the cookie and then redirect to another page, you could use:

print redirect(-uri => 'other_page.html', -cookie => $cookie);
RobEarl
  • 7,862
  • 6
  • 35
  • 50
  • Thanks RobEarl. I fixed this up. I noticed that I didn't need to add`$cookie->bake`. It didn't seem to work when I added that line. I also read the following: `The redirect() function redirects the browser to a different URL. If you use redirection like this, you should not print out a header as well.` (source: http://www.perlmonks.org/?node_id=695252) –  Oct 24 '14 at 09:33
-1

I have same problem, used a simple cgi hello world as following 0end6.cgi

#! /usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body><h1>Hello World!";
print "</h1></body></html>\n"; 

I got error both on browser and error log as AH01264

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
JC Sun
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 27 '21 at 03:48
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/30186745) – Ryan M Oct 27 '21 at 05:29