|
1.
A Basic CGI Program
|
#!/usr/local/bin/perl -w
print <<END_of_HTML;
Content-type: text/html
<HTML>
<HEAD>
<TITLE> Hello</TITLE>
</HEAD>
<BODY>
<H1> Hello</H1>
<P>
Hello World!
</P>
</BODY>
</HTML>
END_of_HTML
|
You
can try this program for yourself by copying the text to your own
public_html directory as basic.cgi, and make sure it is world readable
and executable (chmod 755 basic.cgi) and uploaded in ASCII.
You
can then run it by calling the following URL:
http://www.your-domain.com/basic.cgi (or whatever your domain name
is)
Things
to note
- The
program must be in your public_html directory or any directory
under it.
-
Have the extension .cgi
- Be
executable (chmod 755).
- There
has to be a blank line between the Content-type line and the first
<HTML> tag.
- It
must not contain any tabs or spaces.
Why
does my CGI program not work?
Check
syntax
The FIRST thing to do after writing code is to check the syntax
is correct and you haven't made any basic mistakes (such as missing
semicolons at the end of lines).
At the command prompt, type perl -c myprogram.cgi
Content-type:
header
The most common error in beginners' CGI programs is not properly
formatting the output so the server can understand it. The HTTP
header is very important and you should understand it before progressing.
If
you are really stuck as to why your program doesn't work, try the
CGI: why things don't work home page. The most common problem is
extra whitespace (spaces, tabs, hidden <CR> tags, etc)
Error
Logs
All accesses to the server are logged. If a program fails, an entry
will also be made in the error log. If you run the perl program
with the -w switch, you will get extra warnings to help you debug
any problems.
Your web server logs can be found through the control panel (EziHosting
clients only).
CGI:Carp
You can get your CGI program to output any errors to the browser
window by using the CGI::Carp module. Just add the following line
near the top of any code...
use
CGI::Carp qw(fatalsToBrowser);
This
module arranges to send a minimal HTML page to your web browser
in the event of a fatal runtime or compile error.
Ok,
ready for the next step...
Back - Next
We
hope this tutorial was helpful. If you like, you can link to our
tutorials from your web site. Just use the following code:
<a
href="http://www.ezi-hosting.com/tutorials.htm">Free
Scripting Tutorials from EziHosting</a>
|