我是perl-cgi的新手。我试图连接数据库与cgi脚本我收到此错误

时间:2012-04-18 11:41:18

标签: perl cgi

使用Perl CGI,我正在尝试使用db连接创建登录页面。我正在使用IDE eclipse。在运行它时我收到错误:

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

这是我的代码:

#!/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;

2 个答案:

答案 0 :(得分:-1)

您的子例程sql_connect会在发送正确的HTTP标头之前生成输出(print "...")。) 将调试消息打印到日志文件或首先打印HTTP标头,然后再生成其他内容。

答案 1 :(得分:-2)

当您发送HTTP响应(例如将内容打印到浏览器)时,您的应用程序必须发送响应标题,而不仅仅是正文。当你发送HTML时,你打印到STDOUT的首先应该是一个至少包含这个的标题:

 Content-type: text/html

标题后跟一个空行。

因此,脚本中的第一个打印应该是:

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

(如果您要生成更多标题,则每次只打印一个\ n,直到最后一个。)

当然,如果您使用CGICGI::Simple模块,那么您的生活将变得更轻松。 CGI有许多边缘情况和奇怪的行为已经在这些模块中处理过。