-1

Using Perl CGI, I am trying to create a login page with db connection. I am using the IDE eclipse. While running it I am getting the error:

Server Error
while trying to obtain /sssss/login.cgi
Missing header from cgi output 

Here is my code:

#!/usr/bin/perl
use strict;
use CGI qw(:standard);
use CGI::Pretty qw(:all);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use DBI;
use DBD::mysql;
use DBI qw(:sql_types);
use DBD::ODBC;
use CGI qw/:standard/;
use CGI;


my $cgi = CGI->new();

my $user='root';
my $pass='123';
my $dsn = 'DBI:mysql:delve:server';
my $dbh = &sql_connect;
$dbh-> {'LongTruncOk'} = 1;
$dbh-> {'LongReadLen'} = 90000;
print "Content-type: text/html\n\n";
print "<html><h1>OTT Login</h1></html>\n";
print '<body bgcolor="gray">';


#start a form----------------

print '<form method=POST>';


print '<p>';
print 'Employee Name: <p><INPUT type="text" name="User" size=25 maxlength=25></p>';
print '</p>';

# Create a text box for Password:---------------

print '<p>';
print 'Password:<p><INPUT TYPE=PASSWORD NAME="mypassword" id = "Password" size = "15" maxlength = "15" tabindex = "1"/></p>';
print '</p>';

#Create submit & reset button:-------------------

#print '<p><input type=" button" name="submitit"value="submit"onclick="formvalidation(myform)"/></p>';
print '<form name="input" method="post">';
print '<p><input type="submit" value="Submit" /><INPUT TYPE="reset" name = "Reset" value = "Reset"></p>';

#Create Change Password & Reset Password link:------------
print '<p><a href="changepwd.cgi">Change Password</a></p>';
print '<p><a href="userlogin.cgi">Reset Password</a></p>';
print '</form>';

#logic for submit button functionality :-----------------


if (param('User') and param('mypassword'))
{
my $usr=ucfirst(lc(param('User')));
my $pwd=ucfirst(lc(param('mypassword')));

my $query="select username from login where username='$usr'";
my $data=$dbh->prepare($query)  or die $dbh->errstr;
$data->execute()                or die $data->errstr;
my ($x,$y);
my $query1="select password from login where password='$pwd'";
my $data1=$dbh->prepare($query1)    or die $dbh->errstr;
$data1->execute()               or die $data->errstr;


if ($x=$data->fetchrow())
                {
                 if ($y=$data1->fetchrow())
                 {

                 print "Correct Password";
                 print $cgi->redirect("samp.html");
                 }
                    else
                   {
                   print "Incorrect Password";
                      }
        }
        else
        {

            print "Invalid username";
        }
$dbh->disconnect || die "$DBI::errstr\n";



}
sub sql_connect
{
    Reconnect:
    my $dbh = DBI->connect($dsn, $user, $pass,{AutoCommit => 1}) or warn "$DBI::errstr\a\a\n";

    if(defined $dbh)
    {
        print "Data base Connected successfully\n";
    }
    else
    {
        print "Please Check Ur Database\n"; ### To handle Database Failure
        sleep(10);
        goto Reconnect;
    }
    return $dbh;
}


1;
Sled
  • 18,541
  • 27
  • 119
  • 168
  • 3
    From the [Stack Overflow Perl FAQ](http://stackoverflow.com/questions/tagged/perl?sort=faq): [How can I troubleshoot my Perl CGI script?](http://stackoverflow.com/questions/2165022/how-can-i-troubleshoot-my-perl-cgi-script) - You need to learn how to do basic debugging. Install the required modules. Reduce the program until the problem does not happen anymore to find the part that makes Eclipse think there is no header output. The program works from the command-line, i.e. it print both the necessary HTTP header and some error messages. – daxim Apr 18 '12 at 12:27
  • 1
    If you begin in Perl, avoid CGI if possible. Learn instead a modern web framework such as [Dancer](http://perldancer.org) or [Mojolicious](http://mojolicious.org/). The task will be easier for you (less code to write), and the result will probably be more secure. – dolmen Apr 18 '12 at 12:53
  • You are using `CGI` three times, `DBI` twice. You should cleanup your code. Also it is not recommended to use any of the `DBD::*` modules directly. – dgw Apr 18 '12 at 12:54

2 Answers2

-1

Your subroutine sql_connect generates output (print "...") before the correct HTTP Header is sent.
Either print your debug messages to a logfile or first print the HTTP header before generating other content.

dgw
  • 13,418
  • 11
  • 56
  • 54
-2

When you are sending an HTTP response (e.g. printing things to the browser), your application has to send the response header, and not just the body. As you're sending HTML, the very first thing you print to STDOUT should be a header that contains at least this:

 Content-type: text/html

The header is followed by a blank line.

So the first print in your script should be:

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

(If you have more headers to generate, then print only one \n after each until the last one.)

Of course, if you use CGI or CGI::Simple modules instead, you'll make your life a lot easier. CGI has lots of edge cases and weird behavior that's already been dealt with in these modules.

Darren Meyer
  • 2,251
  • 1
  • 14
  • 8