in reply to Re: Premature end of script headers issue
in thread Premature end of script headers issue

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

Replies are listed 'Best First'.
Re^3: Premature end of script headers issue
by CountZero (Bishop) on Jan 10, 2005 at 21:13 UTC
    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

      Hey, now we have an error!!
      fetch() without execute() at login.cgi line 98.
      I still don't know what it means but the real question is why this could have worked yesterday and NOW it has this problem (whatever it is). It worked, then it stopped working for a day and no one knew what happened. Then it worked for a few days and now it's back to the same problem (I assume it's the same problem).

      What is the error asking for now?

      Thanks very much for your help.

        Somehow the $sth->execute seems to have gotten skipped.

        Change the following

        my $sth = $dbh->prepare("SELECT * FROM $users_table WHERE username = ' +$username' AND user_password = '$userpass'"); $sth->execute;
        to
        my $sth = $dbh->prepare("SELECT * FROM $users_table WHERE username = ? + AND user_password = ?"); $sth->execute($username,$userpass) or die $dbh->errstr;
        It is a more secure way of doing things (protects against arbitrary SQL code injection attacks -- has nothing to do with your present problem)

        The only reason I can see why your script sometimes runs and sometimes doesn't run is in a combination of inputs which skips the execute code. Are you sure all the if ... then ... else ...-logic is OK?

        CountZero

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