LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Html generated by a CGI page counter script displayed in browser without being parsed (https://www.linuxquestions.org/questions/programming-9/html-generated-by-a-cgi-page-counter-script-displayed-in-browser-without-being-parsed-747949/)

gregorian 08-16-2009 12:09 PM

Html generated by a CGI page counter script displayed in browser without being parsed
 
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.

kellinwood 08-16-2009 03:56 PM

You probably need to set the ContentType header.
 
Your problem is probably that the ContentType header of the response from Apache contains "text/plain" instead of "text/html". The browser uses this header field to determine how the page should be displayed. Set the ContentType to "text/html" in the header before the line 'print header;' in your script and it should fix this issue.

gregorian 08-17-2009 08:49 PM

Thank you for your response but the content type isn't text/plain. This is the code generated.

Code:

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Counter trial</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Count: 49</p>
</body>
</html>

P.S. I get the expected result when I save the output as a html file and view it in a browser.

kellinwood 08-18-2009 12:46 AM

I see the content-type meta header in the HTML, but what about the Content-Type header in the HTTP? It is probably not text/html and that's whats confusing the browser. To find out, telnet to the web server (port 80 by default). E.g., "telnet webserver.hostname 80" (replace webserver.hostname with your web server hostname or IP address). Then enter "GET /path/to/your/page HTTP/1.0" (but replace /path/to/your/page with the URI of the page) and press the enter key twice. The server should respond with the HTTP headers, followed by a blank line, and then the HTML you showed above. What does the Content-Type in the HTTP headers say?

gregorian 08-18-2009 09:13 AM

This is what I got:
Code:

me@mycomputer:~$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get /cgi-bin/counter.cgi
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Counter trial</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Count: 62</p>
</body>
</html>Connection closed by foreign host.

I always thought that there was only one Content-Type. And I can see only one content type.


P.S. The other webpages work for some reason.

gregorian 08-19-2009 10:41 AM

Anyone understands why?


All times are GMT -5. The time now is 08:49 AM.