Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The following code no longer works for some reason. It DID work last night and I haven't even opened the file since then and now it says there is a premature end of script headers issue.

I added the print "Content-type" that's commented out to see if that would fix the problem. By removing the #, it no longer errors out but produces a blank screen instead.

My original thought is it must be a database problem but even my database syntax has fail safe error responses. So now I'm really stuck. I either get a blank screen or a premature end of script header error.

Can someone take a guess at what's wrong?

else { # print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); $username = (param('form_user')); $userpass = (param('form_pass')); $userpass = md5_hex($userpass); #check the database my $dbh = DBI->connect("DBI:mysql:$dbase", $mysql_user, $mysql +_pass) or print DBI->errstr; my $sth = $dbh->prepare("SELECT * FROM $users_table WHERE user +name = '$username' AND user_password = '$userpass'"); $sth->execute; if ($sth->rows < 1) { print "Content-type: text/html\n\n"; print "Login information incorrect."; $dbh->disconnect; print "<script>window.location = 'login.cgi';</script>\n"; exit; } else { while ($data = $sth->fetchrow_hashref) { $u_id = $$data{"user_id"}; my $auth_user = new CGI::Cookie(-name => + 'user_id', -value => + $u_id); my $auth_pass = new CGI::Cookie(-name => + 'user_pass', -value => + $userpass); #$cookieset = $auth_user . ";" . $auth_pass; print "Set-Cookie: $auth_user\n"; print "Set-Cookie: $auth_pass\n"; print "Content-type: text/html\n\n"; print "Welcome " . $username . ", you have successfull +y logged in.\n"; print "$u_id = UID"; print "<script>window.location = 'index.cgi';</script> +\n"; } } $dbh->disconnect;

Replies are listed 'Best First'.
Re: Premature end of script headers issue
by amw1 (Friar) on Jan 10, 2005 at 21:00 UTC
    A few things.
    1) Why are you looping across a return set for the page generation? i.e. could the loop run more than once?
    2) It may be worth using CGI's header routines to set the cookies etc.
    3) If you database connection fails you may have a blank page. Try printing $dbh->errstr
Re: Premature end of script headers issue
by CountZero (Bishop) on Jan 10, 2005 at 20:35 UTC
    Difficult to say from the code-snippet you gave. It could have multiple causes.

    In my experience it is most of the time related to an error being generated before the HTML-headers were completed.

    Looking through the web-server error log might help. Adding "warningsToBrowser" could do no harm either. Making sure that you print the headers as soon as possible (CGI.pm header function) and disabling output caching is sometimes a solution.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      I tried to hack the code a little bit from the original and added test prints to see exactly what's wrong. With the following code, it prints tests 1-3 but test 4 never gets printed. So that, I assume, is the main issue with whatever is going wrong.

      It does appear to be a database issue but I'm not very good with them, so can anyone give me an idea of what could possibly be wrong at this point?

      Thanks for your help!

      if (param()) { print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); $username = (param('form_user')); $userpass = (param('form_pass')); $userpass = md5_hex($userpass); #check the database print "test<br>"; my $dbh = DBI->connect("DBI:mysql:$edt_dbase", $mysql_user, $m +ysql_pass) or print DBI->errstr; my $sth = $dbh->prepare("SELECT * FROM $users_table WHERE user +name = '$username' AND user_password = '$userpass'"); $sth->execute; print "test2<br>"; if ($sth->rows < 1) { print "Content-type: text/html\n\n"; print "Login information incorrect."; $dbh->disconnect; print "<script>window.location = 'login.cgi';</script>\n"; exit; } else { print "test3<br>"; while ($data = $sth->fetchrow_hashref) { print "test4<br>"; $u_id = $$data{"user_id"}; my $auth_user = new CGI::Cookie(-name => + 'user_id', -value => + $u_id); my $auth_pass = new CGI::Cookie(-name => + 'user_pass', -value => + $userpass); #$cookieset = $auth_user . ";" . $auth_pass; print "Set-Cookie: $auth_user\n"; print "Set-Cookie: $auth_pass\n"; print "Content-type: text/html\n\n"; print "Welcome " . $username . ", you have successfull +y logged in.\n"; print "$u_id = UID"; print "<script>window.location = 'index.cgi';</script> +\n"; } } $dbh->disconnect; }
        Add an or die $sth->errstr to the fetchrow_hashref to see what went wrong with the call to the Database.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law