LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2009, 12:09 PM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
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.

Last edited by gregorian; 08-16-2009 at 12:12 PM.
 
Old 08-16-2009, 03:56 PM   #2
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
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.
 
Old 08-17-2009, 08:49 PM   #3
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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.

Last edited by gregorian; 08-17-2009 at 08:53 PM.
 
Old 08-18-2009, 12:46 AM   #4
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
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?
 
Old 08-18-2009, 09:13 AM   #5
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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.

Last edited by gregorian; 08-19-2009 at 10:41 AM.
 
Old 08-19-2009, 10:41 AM   #6
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Anyone understands why?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
first cgi script not returning anything on browser linux_cv Linux - Newbie 1 05-28-2009 01:49 AM
PRINTing images in CGI script (HTML tags reqd.) resetreset Programming 3 02-22-2009 10:38 AM
cgi/html/php/something script paul_mat Linux - Software 0 01-23-2006 04:23 PM
Why doesnt my simple html post to cgi script work? ginda Linux - Networking 10 03-15-2005 03:32 PM
Cookie Sharing Between CGI generated HTML pages and standard HTML pages rkwhited Linux - Newbie 5 08-15-2004 07:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration