Home: Perl Programming Help: Beginner:
Premature End of Script Headers Error


Jul 6, 2001, 10:23 AM

Views: 686
Premature End of Script Headers Error

I get a premature end of script headers error on this script. Can anyone help?


$home = "http://www.somewhere.net/";

$redirect = "";

if ($ENV{'REMOTE_HOST'} eq 'aol.com') {$redirect = "http://www.domain.com/aol.html"}
if ($ENV{'REMOTE_HOST'} eq 'syndetics.net') {$redirect = "http://www.domain.com/synd.html"}
if ($ENV{'REMOTE_HOST'} eq 'earthlink.net') {$redirect = "http://www.domain.com/earth.html"}

if ($ENV{'REMOTE_ADDR'} == '') {$redirect = "http://www.domain.com/page1.html"}
if ($ENV{'REMOTE_ADDR'} == '') {$redirect = "http://www.domain.com/anotherpage.html"}
if ($ENV{'REMOTE_ADDR'} == '') {$redirect = "http://www.domain.com/somethingelse.html"}

if ($redirect eq "") {$redirect="$home"}

print "Location: $redirect\n\n";


Jul 6, 2001, 10:39 AM

Views: 684
Re: Premature End of Script Headers Error

Well, for one is this a online type thing(going on a webpage?), if it is you need to include this:
use CGI qw(:all);
print header;
Also one thing i noticed was in your if loops your left out the semicolons:

if ($ENV{'REMOTE_HOST'} eq 'aol.com') {$redirect = "http://www.domain.com/aol.html"; 
if ($ENV{'REMOTE_HOST'} eq 'syndetics.net') {$redirect = "http://www.domain.com/synd.html";
if ($ENV{'REMOTE_HOST'} eq 'earthlink.net') {$redirect = "http://www.domain.com/earth.html";

Other than that i didnt see anything wrong. Hope that helps :)

@letters = ('A'..'Z', 'a'..'z', '1'..'5', '_');
@i = ( '15', '43', '40');
print @letters [ @i, -1, 55];

Enthusiast / Moderator

Jul 6, 2001, 11:10 AM

Views: 680
Re: Premature End of Script Headers Error


Pro_4 is right, you should use the CGI package. Also, you should include CGI::Carp. A nice header for a CGI script to start is normally

#!/usr/local/bin/perl -wT 
use CGI qw(-no_debug :standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;

The -no_debug is optional. It just stops CGI from always asking you for input when you're testing on the commandline.
The CGI::Carp will echo Perl errors to the browser, making script debugging a lot easier.
The use strict; will force clean Perl code.
With the CGI package, you can use

print redirect($redirect);

to print a standard redirection header. I've also noticed that you try comparing IP addresses with ==, but as you actually compare strings, you have to use eq. Using -w would have warned you.
Pro_4, there's no need for semicolons at the end of a block. But if your block isn't a one-liner, I'd recommand putting a semicolon there, too.
Hope this helps,

-- Marcus

(This post was edited by mhx on Jul 6, 2001, 10:38 AM)


Jul 7, 2001, 10:49 PM

Views: 662
Re: Premature End of Script Headers Error

hey dude put this
print "Pragma: no-cache\n";
right b4 the location line