Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Need help with script that writes to a file

error when I pull 2 lines of code



2:30 am on Jul 22, 2007 (gmt 0)

5+ Year Member

Can someone please help. I'm already pulling out what little hair I have left.

When I run the following code it runs fine.


@elements = split /&/, $in;

for $element (@elements)
{$element =~ tr/+/ /; # decode +'s to spaces
($key, $value) = split /=/, $element;
$key =~ s/%([\dA-Fa-f]{2})/pack("C".hex($1))/ge; # decode key
$value =~ s/%([\dA-Fa-f]{2})/pack("C".hex($1))/ge; # decode value

if ($key eq "state") {$state = $value};
if ($key eq "id") {$id = $value};
if ($key eq "dir") {$dir = $value};
if ($key eq "county"){$county = $value};

($day, $month, $year) = (localtime)[3,4,5];
$year = $year+1900;

open(logfile,">> test $year $month.txt");

while($trackrec = <trackfile>)
($trackid,$trackclient,$tracklink) = split(/~/,$trackrec);
if ($trackid eq $id)
{$newrecord = join ("\~",$trackid,$trackclient,$dir,$state,$county,"\n");
print logfile $newrecord;
print "Content-type: text/html\n";
print "Location: [$tracklink\n\n";...]

When I pull out the following lines I get "Premature end of script headers"

print "Content-type: text/html\n";
print "Location: [$tracklink\n\n";...]

I just want to write to the logfile.

Thanks for any help.


4:02 am on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You may find that it works if you leave the Content-Type header in place... It is required (for what, I don't know) even if the script produces no HTTP response output.



8:01 am on Jul 22, 2007 (gmt 0)

5+ Year Member

You do have to print at minimum an http header or the script wil give you that error message, but the script should still work. But you don't need to print a content-type header if you use the print "Location: [$tracklink\n\n";...] construct to print output.


11:41 pm on Jul 22, 2007 (gmt 0)

5+ Year Member

Thanks guys, but I still get the error if I take out either of the lines.


12:17 am on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

If you remove the Location header, make sure the Content-type header ends with "\n\n".



11:10 am on Jul 23, 2007 (gmt 0)

5+ Year Member

in your open statements, use a full system path to the files and don't use spaces in file names, such as

open(logfile,">> /full/path/to/test-$year-$month.txt");

Also, look in your error logs

see if you can find a copy of 'testmy.cgi' on hte internet somewhare and install that in your cgi-bin and runn all your scripts with it.

make sure below is the first 4 lines of your script

#!/use/bin/perl -w

use strict;
use CGI::Carp qw(fatalsToBrowser);

[edited by: Drag_Racer at 11:11 am (utc) on July 23, 2007]


11:17 am on Jul 24, 2007 (gmt 0)

10+ Year Member

If your running this as a CGI program (which it sounds like yo u are) then your program must
1) print a Content-Type header (which gives the MIME type of the data that follows) OR a Location header (which tells the browser to redirct to the URL given)
2) a blank line after the headers before any data

I suspect that when you remove the two header lines, the web server sees no output on STDOUT from the program and so complains that it can't create an HTTP response to send back to the browser - hence the premature end of script headers (which I think is an Apache Web Server error message)


Featured Threads

Hot Threads This Week

Hot Threads This Month