Welcome to WebmasterWorld Guest from 54.145.44.134

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

     

pbrigante

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.

#!/usr/bin/perl

$in = $ENV{'QUERY_STRING'};
@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;
$month=$month+1;

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

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

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.
Paul

jdMorgan

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.

Jim

perl_diver

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.

pbrigante

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.

jdMorgan

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".

Jim

Drag_Racer

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]

simon2263

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