I tried to write a CGI script for a page counter. The script produces correct HTML when I execute it at the shell but the HTML does not get parsed by the browser, it is displayed instead. HTML is parsed by the browser for other scripts (none of them use files).
Code:
#!/usr/bin/perl -wT
use strict;
use CGI ":standard";
my $c = 0;
if(-f "/tmp/count.txt") {
open COUNT, "</tmp/count.txt" or die "Cannot open file for reading";
$c = <COUNT>;
close COUNT;
print $c;
} else {
open COUNT, ">/tmp/count.txt" or die "Cannot open file for writing";
print COUNT $c;
close COUNT;
}
open COUNT, ">/tmp/count.txt" or die "Cannot open file for writing";
print COUNT ++$c;
close COUNT;
print header;
print start_html("Counter trial"),
p("Count: $c"),
end_html;
The HTML generated is correct i.e. the count displayed is one more than the previous count after each run. I'm using Apache (running it through sudo httpd). The page count is stored in a file that has 777 privileges.