Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Premature end of header script

Status
Not open for further replies.

Stainnd

Programmer
Jul 9, 2001
65
0
0
US
Everytime I run this script, I get a "Premature end of header script". Permissions are at 755 so that's not it. Any ideas?

Code:
#!/usr/bin/perl

#Look! Look! This is the password!!!! Shhhhhh! Don't tell anyone!
$user_michael = "***"; 
$password_michael = "***";
#$user_travis = "***"; 
#$password_travis = "***";
#$user_julie = "***"; 
#$password_julie= "***";

# The following code deals with the form data

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

# Get the input

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs

    @pairs = split(/&/, $buffer);

# Load the FORM variables

    foreach $pair (@pairs) {
       ($name, $value) = split(/=/, $pair);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $FORM{$name} = $value;
    }

}


# Does the password equal what the user wrote?
    if(($user_michael eq $FORM{usr})&&($password_michael eq $FORM{pw}))
    {
    $file = "admin.htm";
    open(FILE, $file);
    while(<FILE>)
       {
       print $_;
       }
    close(FILE);
    }
    elsif((($user_travis eq $FORM{usr})&&($password_travis eq $FORM{pw}))||(($user_julie eq $FORM{usr})&&($password_julie eq $FORM{pw})))
    {
    $file = &quot;reviewers.htm&quot;;
    open(FILE, $file);
    while(<FILE>)
       {
       print $_;
      }
    close(FILE);
   }

#Otherwise, print out this page
    else
    {
    print &quot;Content-type: text/html\n\n&quot;;
    print &quot;<HTML>&quot;;
    print &quot;<TITLE>Wrong!</TITLE>&quot;;
    print &quot;<BODY BGCOLOR=#FFFFCC>&quot;;
    print &quot;<CENTER><FONT COLOR=red Size = +4> Incorrect Password</FONT></CENTER><P>&quot;;
    print &quot;</BODY>&quot;;
    print &quot;</HTML>&quot;;
    }

Thanks

-Mike
 
Just something that pokes my eyes, not sure if it will help.


while(<FILE>)
{
print $_;
}

You are printing $_ before you write your headers, you write headers in your ELSE clause not your IF. You need print &quot;Content-type: text/html\n\n&quot;; before printing.
 
You should place your Content-type at the head of your script.

Also.. Instead of re-inventing the wheel use the CGI.pm to parse incoming data instead.

---------------------

# Does the password equal what the user wrote?
if(($user_michael eq $FORM{usr})&&($password_michael eq $FORM{pw}))
{
$file = &quot;admin.htm&quot;;
open(FILE, $file);
while(<FILE>)
{
print $_;
}
close(FILE);
}

M. Brooks
X Concepts LLC
 
Yeah, especially since CGI.pm's wheel is nice and round, and not all blocky and bumpy like that one. We've all seen that code a few too many times, as it seems to be in every Perl/CGI tutorial in existance. Really, CGI.pm makes everything easier, stabler, and less prone to errors and attacks.

But most often, &quot;premature end of script headers&quot; means that you either didn't print a proper http header, or printed an incorrect one.

Happy coding.

________________________________________
Andrew - Perl Monkey
 
Not that it matters so much really, but just wanted to put another 2 cents in. I never use param('') or form(''), I like to stick everything into variables at the top of the script and work from there.

my $user = param('user');
or
my $user = $form{'user'};

my $pw = param('pw');
or
my $pw = $form{'pw'};


if(($user_michael eq $user)&&($password_michael eq $pw))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top