cant get this to display on webpage. Apache is setup correctly. Getting error "malformed header from script. Bad header=04/19/2010: date.pl"

where am i going wrong?

#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);

my $cur_time = strftime "%a %b %e %H:%M:%S %Y", localtime;
print "$cur_time\n";

open PS, "/bin/netstat -nra 2>&1|" or die "failed with : $1";
while(<PS>)
{
	#print;
	$output .=$_;
}
print $output;
close (PS);

print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD>\n<TITLE>Netstat Sample </TITLE>\n</HEAD>\n"; 
print "<strong>";
print "<br>Curent time is <font color=\"#FF0000\">$cur_time</font> MST";
print "</br>";
print "<br>$output</br>";
print "</strong>";
print "</body>\n</html>";

This worked OK for me after commenting out a couple of print statements that were trying to send output to the web page before printing the html header.

#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
my $output;
my $cur_time = strftime "%a %b %e %H:%M:%S %Y", localtime;
#print "$cur_time\n"; #Don't print to webpage before printing your header

open PS, "/bin/netstat -nra 2>&1|" or die "failed with : $1";
while(<PS>)
{
	#print;
	$output .=$_;
}
#print $output; #Don't print to webpage before printing your header
close (PS);

#The first print statement must contain your header info plus two newline characters (\n\n)
print "Content-type: text/html\n\n"; #This has to contain the first lines you send to your web page
print "<HTML>\n";
print "<HEAD>\n<TITLE>Netstat Sample </TITLE>\n</HEAD>\n"; 
print "<strong>";
print "<br>Curent time is <font color=\"#FF0000\">$cur_time</font> MST";
print "</br>";
print "<br>$output</br>";
print "</strong>";
print "</body>\n</html>";

Since you're using strict, you have to scope the variables.

$output

is not scoped. Try using:

my $output;

Outside the WHILE. Also, you can't print anything BEFORE the header. You are printing cur_time which munges the header.

Your entire issue is here

my $cur_time = strftime "%a %b %e %H:%M:%S %Y", localtime;
print "$cur_time\n";
 
open PS, "/bin/netstat -nra 2>&1|" or die "failed with : $1";
while(<PS>)
{
	#print;
	$output .=$_;
}
print $output;
close (PS);

You can't print to the page before you print the content type to the page. Try moving the content type to before the above code (probably should move the <HTML> and <HEAD> declarations too but its not necessary.

Your entire issue is here

my $cur_time = strftime "%a %b %e %H:%M:%S %Y", localtime;
print "$cur_time\n";
 
open PS, "/bin/netstat -nra 2>&1|" or die "failed with : $1";
while(<PS>)
{
	#print;
	$output .=$_;
}
print $output;
close (PS);

You can't print to the page before you print the content type to the page. Try moving the content type to before the above code (probably should move the <HTML> and <HEAD> declarations too but its not necessary.

This article has been dead for over six months. Start a new discussion instead.