in reply to Re: Re: Premature End of Script Headers (was: Re: Newbie error management question) (code)
in thread Newbie error management question

The error reads "error reading the headers", not "Premature end of Script Headers". I have seen both...is there a difference?

Thanks again for the help. Here is the code in question. Please be merciful:

#!/usr/bin/perl -w # NEEDED Variables $basedir = "/home/**/**"; $linkscgi = "http://www.foo.cgi"; # use form to get the data use CGI qw(:standard); $query = new CGI; # enable MySQL database use DBI; $dsn="DBI:mysql:database=foo"; $client_email = $query->param('email'); chomp($client_email); $new_url = $query->param('url'); chomp($new_url); $new_title = $query->param('title'); print "Content-Type: text/html\n\n"; $|++; $mailprog = "/usr/sbin/sendmail"; $LOC = "ALL"; $TRACKER = $ENV{'HTTP_REFERER'}; if (!($TRACKER)) { $TRACKER = $ENV{'REMOTE_ADDR'}; } #################################### # CHECK Entries @LINE= split (/"/,$new_url); $new_url = $LINE[0]; $new_title =~ s/<[^>]*>//g; $new_title =~ s/\/[^>]*>//g; $new_title = &convert_bad_chars($new_title); $new_url = &convert_bad_chars($new_url); if ($new_url eq 'http://' || $new_url !~ /^(f|ht)tp:\/\/(\w|\-)+\.\w+/ +) { &no_url; } if (!($new_title)) { &no_title; } if (!($client_email)) { &no_emailid; } if ($client_email !~ /^[\w\d][\w\d\,\.\-]*\@([\w\d\-]+\.)+([a-zA-Z]{3} +|[a-zA-Z]{2})$/) { &no_emailid; } open (FILE, "badurls.txt"); flock (FILE, 2); @banURL = grep{/$LINE[0]/} <FILE>; flock (FILE, 8); close (FILE); if (@banURL) { &not_allowed($LINE[0]); } #check against 'bad-email' list open (FILE, "bademails.txt"); flock (FILE, 2); @banEMAIL = grep{/$client_email/}<FILE>; flock (FILE, 8); close (FILE); if (@banEMAIL) { &not_allowed($client_email); } #check to see if code is unique $flag = 1; $dbh = DBI->connect($dsn,'foo','foo') or die "Can't connect to MyS +QL database."; while ($flag == 1) { $CODE = &gen_code; $cursor = $dbh->prepare("SELECT count(*) FROM s_table WHERE ID +_Code = '$CODE'"); $cursor->execute(); while (my $get = $cursor->fetchrow_hashref()) { $check = $get->{'count(*)'}; } if ($check==0) { $flag = 0; } } $cursor->finish(); $dbh->disconnect; # Enter MySQL and Add Entry.... $dbh = DBI->connect($dsn,'foo','foo') or die "Can't connect to MyS +QL database."; while ($flag == 1) { $cursor = $dbh->prepare("INSERT INTO s_table (ID_Code,Email,Ur +l,Title,Section,Location,Tracker,Date) VALUES ('$CODE','$client_email','$new_url','$new_title','$ +where','$LOC','$TRACKER', NOW())"); $cursor->execute(); #check to see if entry was accepted $cursor = $dbh->prepare("SELECT count(*) FROM s_table WHERE ID +_Code = '$CODE'"); $cursor->execute(); while (my $get = $cursor->fetchrow_hashref()) { $check = $get->{'count(*)'}; } if ($check) { $flag = 0; } } $cursor->finish(); $dbh->disconnect; ####### # convert title,url $new_title =~ s/\\//g; $new_url =~ s/\\//g; $date= localtime; #Send Email... open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n"; print MAIL "To: $client_email\n"; print MAIL "From: foo\n"; print MAIL "Subject: Your entry\n\n"; print MAIL "Thank you for adding your entry.\n"; print MAIL "\n---------------------------------------------------- +--\n"; print MAIL "Your Link:\n\n"; print MAIL "$new_title\n"; print MAIL "$new_url\n"; print MAIL "Section: $where\n\n"; print MAIL "Submitted on $date\n"; print MAIL "\n"; close (MAIL); print "<html><head><title>Output stuff...</title></head>\n"; print "Thanks!</body></html>"; exit(0); sub no_url { print "<html><head><title>ERROR: No URL</title></head>\n"; print "<body bgcolor=#FFFFFF text=#000000>"; print "<center><h1>No URL $new_url</h1></center>\n"; print "You forgot to enter a url you wanted added to the "; print "link page. Another possible problem was that your link "; print "was invalid.<p>\n"; print "<form method=POST action=\"$linkscgi\">\n"; &get_fields; print "<input type=submit> * <input type=reset>\n"; print "<hr>\n"; print "</form></body></html>\n"; exit(0); } sub no_illegal_chars { print "<html><head><title>ERROR: No URL</title></head>\n"; print "<body bgcolor=#FFFFFF text=#000000>"; print "<center><h1>No Illegal Characters</h1></center>\n"; print "Your entry contains characters that are illegal to use."; print "<p>\n"; print "<form method=POST action=\"$linkscgi\">\n"; &get_fields; print "<input type=submit> * <input type=reset>\n"; print "<hr>\n"; print "</form></body></html>\n"; exit(0); } sub no_title { print "<html><head><title>ERROR: No Title</title></head>\n"; print "<body bgcolor=#FFFFFF text=#000000>"; print "<center><h1>No Title</h1></center>\n"; print "You forgot to enter a title you wanted added to the "; print " link page. Another possible problem is that your title "; print "contained illegal characters.<p>\n"; print "<form method=POST action=\"$linkscgi\">\n"; &get_fields; print "<input type=submit> * <input type=reset>\n"; print "<hr>\n"; print "</form></body></html>\n"; exit(0); } sub no_emailid { print "<html><head><title>ERROR: No EMail Id</title></head>\n"; print "<body bgcolor=#FFFFFF text=#000000>"; print "<center><h1>No E-mail ID</h1></center>\n"; print "You forgot to enter your e-mail id. "; print "Another possible problem is that your e-mail id "; print "contained illegal characters.<p>\n"; print "<form method=POST action=\"$linkscgi\">\n"; &get_fields; print "<input type=submit> * <input type=reset>\n"; print "<hr>\n"; print "</form></body></html>\n"; exit(0); } sub get_fields { print "\nEMail: <input type=text name=\"email\" value=\"$client_ema +il\" size=30><br>"; print "\nTitle: <input type=text name=\"title\" value=\"$new_title\ +" size=40><br> "; print "\nURL: <input type=text name=\"url\" value=\"$new_url\" size +=55><br>"; print "<input type=hidden name=\"section\""; print "value=\"$where\">\n"; if ($LOC ne "ALL") { print "<input type=hidden name=\"lnkuser\""; print "value=\"$LOC\">\n"; } } sub not_allowed { $error = $_[0]; print "<html><head><title>ERROR: URL Entry Denied</title></head>\n" +; print "<body bgcolor=#FFFFFF text=#000000>"; print "<center><h1>URL Entry Denied</h1></center>\n"; print "<center>Sorry, $error\n"; print "You cannot add this URL to this page.<p>\n"; print "</center>"; print "</body></html>\n"; exit(0); } sub gen_code { my ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0, +1,2,3,4,5,6]; if ($sec<10) {$sec = "0".$sec;} if ($min<10) {$min = "0".$min;} if ($hour<10) {$hour = "0".$hour;} if ($mday<10) {$mday = "0".$mday;} my $RND = int(rand(1000)); my $CODE = "$mday$hour$min$sec$RND"; return $CODE; } sub convert_bad_chars { my $string = $_[0]; $string =~ s/'/\\'/g; $string =~ s/"/\\"/g; return $string; }

Replies are listed 'Best First'.
(ar0n) Re: error management...
by ar0n (Priest) on May 15, 2001 at 22:57 UTC
    A simple Google Search came up with this. According to this, that message appears when your headers are too large (over 8k). It also can happen when your request times out.

    And relatively unrelated to your problem: please check out HTML::Template. It'll really help you maintain all the html embedded in your code...

    ar0n ]

Re: error management...
by Sifmole (Chaplain) on May 15, 2001 at 22:34 UTC
    Well, I don't see anywhere where you are:
    print $cgi->header();
    or the like -- so that would be the first thing I would check out.

    To further test things I would run the code at the command line and see what is actually getting printed out. This should illuminate alot. You can run it at the command line by doing..

    ./foo.cgi var1=val1 var2=val2
    where "foo.cgi" is your actual program name; "var1" etc, are each of the variables you are referencing; and "val1" etc, are values to test in those variables.

    The script should then run and you will be able to see what is trying to be sent to the browser.. you will like see something strange appear -- and that will likely be your error.

      He's not using CGI (which you really should do for anything of any size... check use CGI or die; for why), he's printing the header manually, and it looks like it's right.

      Given that, it's likely an error message of some kind printing to STDERR before you print the header, shambright--is there anything else in the error log entries that might indicate that? Usually when I pull that stunt, it comes out with something like "failed to emit valid header (got 'Use of uninitialized value at foo.pl line 34')" .



      If God had meant us to fly, he would *never* have give us the railroads.
          --Michael Flanders

        So much for my reading comprehension, I missed the
        print "Content-type: text/html\n\n";
        But he is using CGI, check at the start of the code.
        #!/usr/bin/perl -w # NEEDED Variables $basedir = "/home/**/**"; $linkscgi = "http://www.foo.cgi"; # use form to get the data use CGI qw(:standard); $query = new CGI;
        Now I am going to have to look a little closer
        All right, I think I am making progess.... I ran the script from the command line, (using the suggestion from Sifmole, thank you)

        To avoid "use of unintialized value" errors, I had to add some lines to check for NULL occurances of the form variables (and $TRACKER - which gave a NULL value from the command line{???}):

        $TRACKER = $ENV{'HTTP_REFERER'}; if (!($TRACKER)) { $TRACKER = $ENV{'REMOTE_ADDR'};} #added if (!($TRACKER)) { $TRACKER = ""; } # why would these ENV variables BOTH be blank ??? $client_email = $query->param('email'); #added if (!($client_email)) { $client_email=""; } chomp($client_email); #added to other query vars...

        This seems somewhat redundant, BUT... those first few variables were happening BEFORE I sent the headers.

        is there a better way to assure that the form values that are needed are not NULL before proceeding?