Search code examples
perlcgi

Debug CGI script that does not print HTML


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...  
   }
}

Solution

  • 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);