homepage Welcome to WebmasterWorld Guest from 204.236.254.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Need help with script that writes to a file
error when I pull 2 lines of code
pbrigante




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

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




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

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




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

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




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

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

jdMorgan




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

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

Jim

Drag_Racer




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


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




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

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)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved