













|
|
 How to create dynamic web content and CGI
Sometimes you need to generate a web page (or part of it)
dynamically - for example, to display the current date / time,
or the count of visitors. Some pages contain forms with
input fields and buttons, and you need to process the information
entered in the form. We have some commonly used programs which
may be useful to you: form processing, counters, etc. This document
assumes that you are familiar with the HTML language, or at least
you know how to insert HTML source code in your web page.
The following information contains texts specific for
the OS platform of your web server. If you are not sure if your
web server is on a Unix or NT platform please check with
webmaster@gekko.net
Using JavaScript
JavaScript is a scripting language which is interpreted by
the web browser. Currently, only
Netscape Navigator and
Microsoft Internet Explorer
support JavaScript. JavaScript is derived from the Java language
and is close to C++. JavaScript code is embedded in the HTML within
the <SCRIPT></SCRIPT> tag. Here is an example
which displays the current date in your page:
Here is the output of the script:
Today is 11/5/97
Your browser is Netscape 4.03 [en] (WinNT; U)
The complete JavaScript manual can be found at
Netscape's web site.
Using Server Parsed HTML
Most UNIX web servers can pre-process HTML files. This feature
has to be configured on the server. Usually the server would
recognize files with special extensions (usually .shtml).
The HTML syntax for server-parsed commands is
<--#command attribute=value attribute=value ... -->
The following example outputs todays date:
More detailed information can be found in the
Apache server
documentation.
Using CGI scripts (Perl, Unix shell, etc.)
The most common way to create dynamic content is to use CGI - the
web server executes a program which generates HTML page as output.
There are two methods used to pass information (such as the content
of the input fields in a form) to the program: GET and POST.
The information is encoded according to the
CGI protocol and needs to be
decoded. There are common libraries for Perl, C, etc. for handling CGI input.
Click here to download
a Perl CGI library.
In your web page you need to reference the CGI either as
link or as ACTION in the form tag if the CGI is supposed to
handle a form. Example:
On NT web servers script file extensions are associated with
the interpreter for the language (e.g. .pl files are associated
with Perl and will be directly executed).
On Unix web servers the scripts need to begin with #! and the
interpreter location (e.g. Perl scripts must start with the line
#!/usr/bin/perl5 ). Also, the scripts must have executable
permissions for the web server - after you uploaded the scripts,
telnet to your web server with your username and password, and change
the permissions with the command 'chmod 755 filename'. Some ftp
programs support changing permissions as well.
Using CGI executables (C, C++, etc.)
Most CGIs are written in interpreted languages and don't need to be
compiled. However, if you are more familiar with another programming language
like C or C++ you might prefer to use it for your CGIs.
On Unix web servers C files are usually compiled with the cc or gcc
cc -o file file.c
For C++ you can use the g++ (GNU C++)
if it's available on the web server.
On NT web servers you need to compile WIN32 executables at home
and upload them.
Protecting files and directories with passwords
Some web servers have features which allow you to protect files and
directories with a password. On Windows NT with the Microsoft IIS server
an actual user has to be created on the system. Please e-mail
support@gekko.net if you'd like
to protect a file or a directory with a password on an NT hosted server.
On Unix, the Apache web server allows to limit the access to
a directory using a .htaccess file. Detailed description of the
.htaccess format can be found at
www.apache.org. Here is a short
example of protecting a directory:
- In the directory which you want to protect, create a .htaccess
file which contains:
AuthUserFile /full/path/to/passfile
AuthName protected area
AuthType Basic
<Limit GET POST>
require valid-user
</Limit>
-
Create a file named passfile, in the format:
username1:password
username2:password
...
Note: It's not a very good idea to put the file with the
passwords in the protected directory like in the example above,
because it could be downloaded.
|