4D Web Serving Using Perl CGIs


ACI - Documentation Français English German ACI Technical Notes ACI Technical Notes, By Subject Back Previous Next Find

4D Web Serving Using Perl CGIs

By Jon Baltazar, 4D, Inc Technical Support

Technical Note 01-7

Technical Notes for Technical Notes for 01-02 February 2001

Introduction


Since the release of 4Dv6.7, 4D Web Serving now offers the ability to use CGI's (Common Gateway Interface). With this new feature that was not offered in previous versions, you can use CGI scripts to perform tasks that were not possible with 4D. CGI's for Web servers are similar to plug-ins for 4D Methods. they are called by the Web server to execute a task and return an answer. They can either result in a full page or some HTML code inserted in the page sent by the server. CGI's are frequently used to display hit counters, generate guest books, question answer forms, etc. The main purpose to use CGI's with 4D is to perform tasks that cannot be done by 4D.

This tech note discusses the use of Perl CGI's with 4D Web Serving. The sample database demonstrates the use of five Perl CGI scripts. Even though some of the Perl scripts being demonstrated are not part of everyday practical use, it is simply a way of showing the use of Perl CGI's and how they can be used with 4Dv6.7. The purpose of this tech note is to show how to call CGI's through HTML tags, and the show the file location of the CGI's

Knowledge of 4D Web Serving


Before you can start using CGI's with 4Dv6.7, you will need to know the basics behind Web Serving with 4D. If you are unfamiliar with how to use 4D as a web server, I recommend that you look sample databases WebAuction and Snap-e. These sample databases demonstrate the use of 4D Web Serving in Non-Contextual Mode.

Web serving can be accomplished in two ways with 4D. The easiest way of doing web serving is in Contextual mode. Contextual mode is when you allow 4D to automatically generate the HTML pages.

Once a database has been created in 4D, it can be used as a web server by selecting the "Publish at Startup" in the Database Properties. The limitation to Contextual mode is that the HTML pages are generated by 4D, therefore the developer has no control over the HTML pages. Non-Contextual mode, however does not have 4D automatically generate the HTML pages. This means that the developer must create the HTML pages. Creating the HTML pages from scratch gives the developer the ability to design the pages however they feel fit. It also gives the advantage of adding features that are not available in Contextual mode, features like CGI's. Once the HTML pages have been created, the "Publish Database at Startup", "Start without Context", "Default Html Root", in this case is set to "/" and "Default Home Page" must be set.

Types of CGI's


There are three types of CGI's that can be used to interface with a Web Server, an application, a Perl script or a .DLL. If you wish to use CGI's to interface with your Web server, but do not have enough knowledge of how to create an application, .DLL or write Perl script, many CGI's are now available on the Web, and most of them are freeware.

Installation


After a database has been created in 4D, a cgi-bin folder must be created and placed in the same directory as 4D structure file. Inside this folder is where all the CGI scripts are to be placed. Once the CGI's are placed in the cgi-bin folder, they are then ready to be called from the HTML pages.

CGI's can be called in three ways. They can be call through a URL, an action or through an HTML tag within a page according to the task to be done by the CGI. In any case, however the CGI is called, it must contain /cgi-bin/ followed by the CGI name or possibly a path using the HTML syntax as well as the search string.

For example, a call to a CGI through a URL would look something like this, "http://127.0.0.1/cgi-bin/counter.exe". A call through an HTML tag would look something like, <img src = /cgi-bin/counter.exe>.

Supported CGI Types


Windows

Executables (.EXE) - The source code is usually cross-platform (Windows and Unix). The CGI names are usually written as follows: xxx.exe or nph-xxx.exe.

DLL ISAPI - i.e. extensions when using IIS (Internet Information Server). The CGI names are written as follows: xxx.dll or nph-xxx.dll.

For further information about this type of CGI, please refer to www.microsoft.com/iis/.

Perl Scripts - The CGI's require an interpreter to be executed (ex. ActivePerl). However, they are cross-platform (Windows, Unix and Mac). Their names are written as follows: xxx.pl , nph-xxx.pl, xxx.cgi or nph-xxx.cgi.

Mac OS

Applications - 4D WebSTAR and MacHTTP extensions. Their names are written as follows: xxx.cgi, xxx.acgi.

Perl scripts. These CGI's require MacPerl in order to be executed. Their names are as followed: xxx.pl, nph-xxx.pl, xxx.cgi or nph-xxx.cgi.

Interaction between 4D Web Server and CGI's

______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________A call to a CGI does not modify the 4D Environment (selection, variables, etc)

4D does not have a limit to the response size. However, note that the maximum processing time allocated to a CGI is limited to 30 seconds. After this time, the Web Server will return an error.

A CGI is always executed without context, regardless of its calling mode. Note however, that in contextual mode, we strongly recommend that you not use a CGI sending back HTML code, as it might desynchronize the context.

For further information about 4D Web Server using CGI's please refer to the Web Serving topic in the 4D Language Reference Manual, www.4d.com/ACIDOC/V6U/V6U00055.HTM

Sample Database

______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________In order to see how the sample database uses Perl CGI scripts you must have a Perl interpreter. If you are on a PC, you can use ActivePerl and if you are on a Mac, you can use MacPerl. Both applications can be downloaded from www.downloads.com.

The sample database demonstrates the use of five CGI scripts. The five CGI scripts that are demonstrated are counter.pl, hello_world.pl, qa.pl, redirect.pl and img_counter.pl. Each script is called through an HTML tag, in their own respective ways.

The img_counter is the first CGI script to be executed. Each time a user first hits the homepage

(index.html), the image counter will increase. In the index.html file, this Perl script is called with the following HTML tag:

Notice that the HTML tag is an img src that calls the img_counter.pl from the "cgi-bin" folder. Also, another file called count.txt is called by the, ?file. This indicates that the count.txt file is located in the file folder, and this is a datafile which keeps track of the current hit count. The following is the Perl script for the image counter:

The other scripts can be executed by selecting the provided links. The links take you to a new page where you can execute the CGI and view its source code.

The Script Counter is a Perl CGI that indicates how many times that script has been executed. Once the script has been executed, it will notify you of how many times the script has been called, and will ask you if you wish to run the script again. If you choose to run the script again, you will see that the number increases. The following HTML tag calls the CGI script:

The Perl script is as follows:

The Hello World script executes the very basics of Scripts. This demo is to simply show how a CGI can

be called. Once a call has been made to the CGI, it results in an output simply stating "Hello World".

The following HTML tag calls the script:

The Perl Script is as follows:

The Question/Answer script allows users to submit a question and answer previously posted questions. This CGI allows you to view previously posted questions, people's answers, allow you to post a question(s), and allows you to answer question(s) of your choice. The qa.pl script requires 3 datafiles, ans.txt, many.txt and quest.txt. These datafiles are to be placed in the "file" folder, which is a subfolder of the "cgi-bin" folder on the windows machine, and on Mac these files are placed in the cgi-bin folder.

The CGI script is called by the following HTML tag:

The Perl script is as follows:

*

The redirect.pl CGI displays a dropdown menu that gives the user a list of websites to choose from. Once a selection has been made, click on the "OK" button and the CGI script executes taking you to the desired website. The CGI script is called by the following HTML tag:

The redirect Perl script is as follows:

Conclusion


In conclusion, the purpose of this tech note is to show the use of Perl CGI's with 4D Web Serving. Now that you have read this tech note, you are now ready to add Perl script to your HTML pages. You have the knowledge of what Perl interpreter is needed for the Mac or Windows platform, which CGI types are supported on each platform, how to make calls to CGI's and where the CGI files should be located.


ACI - Documentation Français English German ACI Technical Notes ACI Technical Notes, By Subject Back Previous Next Find