Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple CGI not working 2

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I am new to CGI programming, I would like to create some html pages on the fly through a CGI

I have create the following one:
#!/usr/bin/sh
echo Content-type: text/html;charset=iso-8859-1
echo "<html>"
echo "<head>"
echo " <title> test </title>"
echo "</head>"
echo "<body>"
echo "<H2> TEST </H2>"
echo "</body>"
echo "</html>"

When I call it in the browser I get a smple printout of the file, why?

Note that the cgi file has permissions 777

 
Code:
#!/usr/bin/perl

print <<EOHTML;
Content-type: text/html
  <html>
  <head>
  <title> test </title>
  </head>
  <body>
  <H2> TEST </H2>
  </body>
  </html>
EOHTML

Put your html between print <<EOHTML and EOHTML, this saves you having to write out all those echo statements.

Make sure there is no spaces before or after these lines:
Content-type: text/html
EOHTML

Hope this helps.


Sean.
 
Oh yeah, it was printing out text because you weren't telling your browser where perl was in order to process the script. You had to do that by typing:
#!/usr/bin/perl
At the top of the script.

Sean.
 
thanks but this still does not work. Does this script need to be in cgi-bin directory or can it be called via
file:xxxxx/test.cgi ?
 
Try....

echo "Content-type: text/html\n\n"

note the two "\n"'s
 
Yeah! Sorry, I was typing so fast I didn't even notice!

The two "\n"'s must be there. Here is the proper version:
Code:
#!/usr/bin/perl

print <<EOHTML;
Content-type: text/html\n\n
  <html>
  <head>
  <title> test </title>
  </head>
  <body>
  <H2> TEST </H2>
  </body>
  </html>
EOHTML
OR, if you want to use print or echo statements (I like the look of print myself):
Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title> test </title>\n";
print "</head>\n";
print "<body>\n";
print "<H2> TEST </H2>\n";
print "</body>\n";
print "</html>\n";
Note that there is a "\n" character at the end of every line. This will have no effect on your html code, it will help out when you view the source of the page though. If you leave them out then you source code will be on one long line. If you use my method from the top you do not need to put "\n" at the end of every line. It keeps the format that you give it.

Hope this helps you on your way towards using CGI, believe me it is extremely powerful. I was forced to start learning perl and perl/cgi 4 months back for my new job, and I have learnt alot from the web. Now I have started learning javascript, which is client side scripting. Using a combination of javascript and perl/cgi I can do just about anything!

Good luck for the future.

Sean.
 
It still does not work, I get "internal server error
 
I tried your second version Sean with a print at every line and it works fine now.

Do you know of any good online tutorials on CGI/perl programming?

Thanks!
 
Embedding html inside code is generally a bad habit to get into, using templates makes this significantly cleaner. But for quick and dirty scripts, it's not really neccessary. Still, at the very least, you should probably be using a module to generate your headers (that way you don't have to worry about needing two newlines after the mime instead of one, the module will always make a proper one for you). It's even nice to get in the habit of using the module to generate the html for you, too, as it will always be compliant, too. For example, you could do the above like this:
Code:
#!/usr/bin/perl
use CGI qw/:standard/;

print header(),
      start_html(-title => 'test'),
      h2('TEST'),
      end_html();
See how nice, clean, and maintainable that looks? If something changes in the xhtml or http spec (not likely, but still...) the module changes with it and you never have to touch your code.

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top