Chapter 17

Advanced Windows NT CGI Applications


CONTENTS


The preceding chapter discussed how you can develop CGI applications using Perl and C/C++. In this chapter, you learn how you can use PolyForm, a powerful CGI form utility, and iBasic, a server-side scripting language, to provide dynamic content to users browsing a Web site. The purpose of this chapter is not to cover all aspects of these two applications comprehensively. Instead, you learn how you can use various capabilities of these two applications to provide dynamic content and make a Web site interactive in a matter of minutes.

Creating Interactive Web Pages with PolyForm

PolyForm, from O'Reilly & Associates (ORA), is a useful CGI application that you can use to make a Web site interactive in a matter of minutes. As the name suggests, PolyForm is ideal for creating forms for users to fill in and submit. You can create a typical form application, such as a guest book or e-mail feedback form, with PolyForm in less than five minutes. PolyForm works with both Windows CGI (WinCGI) and Internet Server API (ISAPI) Web servers. At the time of this writing, PolyForm supports the following Web servers:

URL
Visit the following Web site for the most up-to-date information about PolyForm:
http://polyform.ora.com/

Installing PolyForm

Using the PolyForm Installation Wizard, you can install PolyForm in just a few minutes.

URL
You can download an evaluation copy of PolyForm from the following Web page:
http://software.ora.com/download/

After you download the PolyForm distribution file, copy it to a temporary directory and decompress it. After you decompress the distribution file, execute the file setup.exe to begin installing PolyForm. The PolyForm Installation Wizard then pops up and guides you through the installation. The first dialog box asks you to quit all Windows applications before continuing. You click the Next button in this dialog box to acknowledge the warning and proceed to the next dialog box. Before doing so, however, make sure that all Windows applications you can afford to quit are terminated. When you install PolyForm, the installation program needs to copy several shared DLL files to the Windows NT System directory. The PolyForm Install Wizard is unable to do so if other applications are using the shared DLL files that have to be replaced. In this case, you can select to ignore certain DLL files. Doing so, however, may bring about certain undesired results.

Using the Server Software dialog box shown in Figure 17.1, you can select the Web server you are running. PolyForm supports WinCGI- and ISAPI-compliant Web servers. For the purpose of this example, install PolyForm on the MS Internet Information Server, an ISAPI-compliant Web server. After you select your server software, click the Next button to proceed to the next dialog box.

Figure 17.1: The Web Server Software selection dialog box.

The Mail Settings dialog box asks for the Web server's domain name, as shown in Figure 17.2. Type in the domain name (www.server.com) and click the Next button to continue. Use the next dialog box to specify the location of the Web server's CGI directory (H:\Publish\WWW\cgi-bin) and click the Next button. You then are asked for the URL of the CGI directory, which is usually http://www.server.com/cgi-bin.

Figure 17.2: The Mail Settings dialog box. Type in the domain name of the mail server and e-mail address of the PolyForm administration.

After obtaining information discussed previously, the PolyForm Installation Wizard installs PolyForm and configures it with the data supplied. Then the installation program displays PolyForm release notes in a dialog box. You should read the release notes to find out more about PolyForm and whether you need to do anything special to get PolyForm to function with your Web server. The PolyForm installation program also creates a new Windows NT Start Menu folder similar to the one shown in Figure 17.3. PolyForm is now fully installed and ready for use.

Figure 17.3: The PolyForm Start Menu folder.

Understanding Components of PolyForm

PolyForm consists of two applications. Figure 17.4 shows the first application, which you invoke by selecting the PolyForm CGI icon shown in Figure 17.3. Although the PolyForm CGI application has limited functionality, it is convenient for determining how many forms were submitted to the PolyForm CGI application for processing. The box next to POSTs contains the number of form submissions. You can reset this value by double-clicking it.

Figure 17.4: The PolyForm CGI Application.

You use the PolyForm Control Panel application, shown in Figure 17.5, to create and edit PolyForm forms. You can invoke it either by selecting the PolyForm Control Panel icon in Figure 17.3 or by choosing Control | Control Panel from the dialog box shown in Figure 17.4.

Figure 17.5: PolyForm Control Panel.

Creating a Feedback Form with PolyForm

Creating a feedback form with PolyForm is easy. To begin creating a feedback form, invoke the PolyForm Control Panel by double-clicking on the PolyForm Control Panel icon. You then see a dialog box similar to the one shown in Figure 17.5. You can create forms with PolyForm in two ways. You can either use the PolyForm Wizard or the PolyForm Control Panel shown in Figure 17.5. Either way, you can customize the form using the PolyForm Control Panel. I recommend that you first use the PolyForm Wizard to create a form and then use the PolyForm Control Panel to customize it. Choose File | Script Wizard (or click the blue wizard hat icon) to invoke the PolyForm Script Wizard shown in Figure 17.6.

Figure 17.6: The PolyForm Script Wizard.

In the PolyForm Script Wizard, click the Next button to advance to the next dialog box. Using the dialog box shown in Figure 17.7, you can select the type of form to create from several different form types. Select Feedback Form from the drop-down list, and click the Next button to advance to the next dialog box.

Figure 17.7: Selecting the type of form.

In this dialog box, you can save the contents of the form in one of three ways. You can save the contents as a text file, a data file, or not at all. The option to save the form contents as data is useful if the data in the form is to be exported to an application such as MS Access or Excel at a later time. Because the feedback form is going to be e-mailed, the Don't Save option is selected in the dialog box shown in Figure 17.8. Press the Next button to continue creating the feedback form.

Figure 17.8: Options for saving form contents.

After the form is submitted, you can use the dialog box shown in Figure 17.9 to send a customized Web page to the browser. For the purpose of this example, send a standard conformation message to the user. After selecting an option, click the Next button. Note that you can modify this setting to send a customized Web page, at a later time, by using the PolyForm Control Panel.

Figure 17.9: Selecting a response to browser.

Using the dialog box shown in Figure 17.10, you can specify various e-mail options. When the form is e-mailed by PolyForm, information in this dialog box is used to customize the e-mail message. Make sure that the Copy Form Contents to check box is checked before proceeding to the next dialog box.

Figure 17.10: E-mailing options.

Use the dialog box shown in Figure 17.11 to name the PolyForm script. This name identifies the PolyForm script that you will create shortly. Give a descriptive name so that you won't have any trouble locating it at a later time. Note that PolyForm script names cannot contain any spaces.

Figure 17.11: Naming the PolyForm script.

After all the data is entered, PolyForm displays a data confirmation dialog box, as shown in Figure 17.12. Use this opportunity to make sure that data supplied to the PolyForm Script Wizard is accurate. After you verify the information, click the Next button to create the PolyForm form.

Figure 17.12: Script settings confirmation dialog box.

After it creates the script, PolyForm displays a dialog box identical to the one shown in Figure 17.13. Click the Finish button to exit the PolyForm Script Wizard and return to the PolyForm Control Panel. After you exit the PolyForm Script Wizard, you can use the PolyForm Control Panel to customize various script settings. If you make any changes to the script, be sure to choose File | Save from the main menu to save the changes.

Figure 17.13: The feedback form is now ready for use.

Customizing PolyForm Scripts

On the Forms tab of the PolyForm Control Panel shown in Figure 17.14, you can customize and create PolyForm scripts. I recommend that you place a checkmark next to the Allow only this Form to run this Script check box. If this check box is not checked, a user can potentially make a copy of one of your forms, make changes to it, fill it with some data, and submit it. This action can lead to undesired consequences because the order and existence of various fields can no longer be guaranteed when a form is tampered with.

Figure 17.14: You can use the PolyForm Control Panel to customize PolyForm scripts.

On the Save to File tab of the PolyForm Control Panel shown in Figure 17.15, you can configure how data submitted with a form is saved. You can save the data as a text file, data file (for exporting to a database), or a template (for exporting to an application such as MS Word). If you're using a feedback form, and its contents will be e-mailed by PolyForm, you can select to discard the data, as shown in Figure 17.15.

Figure 17.15: Configuring how data submitted is saved to a file or discarded.

You can send a customized Web page to the user who submitted the Form using the Send to Browser tab of the PolyForm Control Panel, as shown in Figure 17.16. I recommend that you use this feature to send a customized Web page to the user. Be sure to include a link in the customized Web page so that the user can select to continue navigating your Web site. Don't let users use the Web browser's back arrow button to go back to a previous page after the form is submitted. This gives the impression that your Web site was not put together in a thoughtful manner.

Figure 17.16: Specifying that a Web page be sent to a browser after the often form is processed.

Using the Email Options tab of the PolyForm Control Panel, you can customize e-mail messages sent by PolyForm. (See Figure 17.17.) In addition to sending the contents to a certain user, you also can send a copy of the e-mail message to the user who submitted the Form by placing a checkmark on the Letter to user check box. Note that if this check box is checked, the Form should have an <INPUT NAME="E-Mail"> field; this field is case-sensitive.

Figure 17.17: Customizing e-mail options.

Testing Feedback Forms Created with PolyForm

Refer to PolyForm documentation for more information about using various advanced features of PolyForm to create interactive Web pages. You can access the feedback form that you just created by using a Web browser by looking up the URL http://wonderland.dial.umd.edu/cgi-bin/PolyForm.dll/SimpleFeedbackForm. Note that if your Web server does not support ISAPI, the URL contains PolyForm.exe instead of PolyForm.dll. The form created by the PolyForm Script Wizard is shown in Figure 17.18. PolyForm forms are located in the directory PFTOOLS\forms and can be customized to suit your needs. You might want to add a few extra fields, and perhaps, a graphic or two.

Figure 17.18: The completed feedback form.

After the feedback form is submitted, because a simple confirmation message is selected in the Send to Browser tab of the PolyForm Control Panel (see Figure 17.16), PolyForm echoes the contents of the form back to the browser, as shown in Figure 17.19.

Figure 17.19: The feedback confirmation message.

The e-mail message sent by PolyForm to the address specified in the Email Options tab (see Figure 17.17) is shown in Figure 17.20. Note how information typed into the form in Figure 17.18 is formatted by PolyForm. As just demonstrated, PolyForm can be used to create interactive Web pages effortlessly without spending hours or even days creating forms with a programming language such as C or Perl.

Figure 17.20: Contents of the e-mail messages mailed by PolyForm. Note how information submitted using the Web page in Figure 17.18 is formatted by PolyForm.

Creating a URL Selector with PolyForm

You might have noticed how some Web pages use a drop-down list from which you can select various URLs. After selecting a URL and clicking on a button, you are automatically transferred to the URL that you selected. You can implement such a drop-down list with PolyForm in a couple of minutes. When you create a drop-down list of URLs, you do not have to specify a script name. The drop-down list of URLs created with the HTML code in Listing 17.1 is shown in Figure 17.21.

Figure 17.21: You can use PolyForm to implement a drop-down list of URLs.


Listing 17.1. Drop-down URL list demonstration.

<HTML>
<TITLE>Drop down URL list demonstration</TITLE>

<BODY BGCOLOR=FFFFFF>

<HR>
It is very easy to implement a drop-down list of URLs with
PolyForm. Please select a URL from the list below and
click on the "Please take me there" button.<P>
<HR>
<FORM METHOD="POST" ACTION="/cgi-bin/polyform.dll">
<select name="URL" size=1>
<option value="http://www.NetInnovation.com"> NetInnovation.COM
<option value="http://www.Microsoft.com"> Microsoft Web site.
<option value="http://www.mcp.com"> Macmillan Web site.
<option value="http://polyform.ora.com"> PolyForm Web site.
</select>
<input type="submit" value="Please take me there!">
</FORM>
<HR>

</BODY>
</HTML>

Where to Go from Here

In the preceding sections, you learned some of PolyForm's functionality and how you can use it to create interactive Web pages. Take some time to go over the PolyForm documentation to learn more about its features. PolyForm is a general-purpose CGI application that you can use for a wide variety of tasks, in addition to creating feedback forms. Take the time to learn some of its advanced features such as the capability to export data into MS Access, Excel, and other spreadsheet or database applications. You then make your Web site interactive, and automate various tasks that would otherwise have to be done manually.

Creating Dynamic Web Pages with iBasic

Although you can use C and C++ to create custom CGI applications, these programming languages were not designed to be used for CGI programming applications. iBasic is a powerful scripting language that is ideal for creating CGI applications. The purpose of this section is not to cover all aspects of the iBasic language comprehensively. Instead, it is to provide you with an overview of iBasic and some of its capabilities. Refer to the iBasic documentation for additional information about iBasic.

URL
Visit the following Web site for the most up-to-date information about iBasic:
http://www.ibasic.com/

Installing iBasic

Installing iBasic on your system is easy as long as your Web server supports ISAPI or CGI. The iBasic scripting language and server extensions are contained in two files.

URL
The iBasic (server-side scripting tool) download Web page:
http://www.ibasic.com/Download.html

After you download both files, create two temporary directories and copy each file to its own directory. Before you proceed to install iBasic, note the temporary evaluation key given at the Web site from which iBasic was downloaded. This key is required to install iBasic on your system.

Installing the iBasic Scripting Language

After you copy the iBasic scripting language distribution file to a temporary directory, execute it to decompress the distribution file. Then execute the file setup.exe to begin installing iBasic. The installation program first presents you with a license agreement. Read it and proceed to the next dialog box. This dialog box asks for the product key. Type in the temporary evaluation key obtained from the Web page from which iBasic was downloaded to continue installing iBasic. You can select various iBasic components to be installed using the Select Components dialog box shown in Figure 17.22. I strongly recommend that you select all components in this dialog box before moving on to the next dialog box.

Figure 17.22: The iBasic component selection dialog box.

Using the Choose Destination Directory dialog box shown in Figure 17.23, you can select the destination directory of iBasic. Note that this directory does not have to be a child of your Web server's document root directory. After you select a destination directory, proceed to the next dialog box.

Figure 17.23: Selecting destination directory of iBasic.

The iBasic installation program uses the information from the Web Server Selection dialog box shown in Figure 17.24 to find out the Web server you will be using with iBasic. As you can see, iBasic supports several Windows NT Web servers, including all four Web servers covered in this book. Select your Web server from the list of supported Web servers and proceed to the next dialog box.

Figure 17.24: Selecting the type of Web server.

The installation program uses the information from the Web Server Settings dialog box to obtain the document root directory, the CGI directory, and the domain name of your Web server. Type in the requested information, as shown in Figure 17.25, and proceed to the next dialog box to begin installing iBasic.

Figure 17.25: The Web Server Settings dialog box.

After it installs the iBasic scripting language, the installation program insists that you reboot your server before using iBasic. I recommended that you follow this advice and reboot your server before continuing. By rebooting, you can ensure that iBasic is fully installed on your system.

Installing iBasic Extensions

After you install the iBasic scripting language, log on to the temporary directory to which the iBasic extensions file is copied. Then execute the distribution file to decompress it and execute the file setup.exe to begin installing iBasic. First, you are presented with a dialog box displaying the iBasic license agreement; read it and proceed to the next dialog box. Using the Select Components dialog box shown in Figure 17.26, you can select various iBasic Server Extensions' components to install. I recommend that you select all components in this dialog box before continuing to make maximum use of various iBasic features.

Figure 17.26: iBasic Server Extensions' component selecting dialog box.

In the Choose Destination Directory dialog box, displayed in Figure 17.27, you can select the destination directory of iBasic Server Extensions. After you type in the destination directory, the installation program installs iBasic server extensions on your system.

Figure 17.27: iBasic Server Extensions' destination directory selection dialog box.

After it installs iBasic, the installation program adds an iBasic application icon to the Windows NT Start Menu, as shown in Figure 17.28. Proceed to the next section to learn how you can use iBasic to provide dynamic content to users browsing your Web site. Reboot your server and then make sure that it is functioning normally before you proceed to the next section.

Figure 17.28: After iBasic is installed, an iBasic application icon is added to the Windows NT Start Menu folder.

Using the iBasic Development Environment

The iBasic development environment is thoughtfully laid out. Using it, you can easily create iBasic applications. You can execute the iBasic application, hereafter called the iBasic Workbench, by selecting it from the Windows NT Start Menu. As shown in Figure 17.29, the iBasic Workbench comprises two panes. The left pane displays various files and directories, and the right pane displays the file selected.

Figure 17.29: The iBasic Workbench.

Notice the three tabs at the bottom of the right pane of the iBasic Workbench. These tabs are useful when creating iBasic applications. You can use the Editor tab to edit iBasic applications, as shown in Figure 17.29. Figure 17.30 demonstrates how you can use the Exe tab to view Basic source code of iBasic applications. If you compare Figure 17.29 to Figure 17.30, you will notice that the iBasic application in Figure 17.29 has been translated into a Basic application in Figure 17.30. iBasic is compatible with Visual Basic for Applications (VBA).

Figure 17.30: Exe tab of the iBasic Workbench.

A Web browser also is included with iBasic. Because it is integrated with the iBasic Workbench, you can use the Web browser included with iBasic to test iBasic applications. You do so by selecting the application in the left pane and clicking the Browser tab. Figure 17.31 demonstrates how you can test the Birthday application included with iBasic using the built-in Web browser.

Figure 17.31: The Web Browser tab of the iBasic Workbench.

Hello World, iBasic!

If you are familiar with Visual Basic, the iBasic code in Listing 17.2 should look familiar to you. If not, spend some time with the iBasic documentation to become more familiar with it. You can access iBasic documentation by choosing Help|Contents from the iBasic Workbench main menu.


Listing 17.2. "Hello World" iBasic script.

<HTML>
<TITLE>
Hello World, iBasic!
</TITLE>

<BODY BGCOLOR=FFFFFF>
<H1>
Hello World!
</H1>

<HR>

<P>Welcome to the fun filled and dynamic world
of iBasic programming!

<P>Today is %Format(Date(), "mmmm d, yyyy")%,
and time is now %Time()% EDT.

{
For count = 1 To 10
  For countTwo = 1 To count
    Response.Send "*"
  Next countTwo
  Response.Send "<BR>"
Next count
}

<HR>

</BODY>

</HTML>

Refer to Figure 17.32 for the output of the Hello World application. It simply displays the string "Hello World" and the current time of the Web server. In addition, it uses a simple loop to draw a triangle of asterisks. If you look at the iBasic code listing, you will notice that it looks similar to HTML code. The only difference is the loop structure enclosed with curly braces, and time and date functions enclosed within two percentage signs. As demonstrated in this simple example, iBasic functions can be used to create dynamic Web pages by enclosing them with percentage signs. You can enclose longer iBasic code listings, like the one that draws the triangle, within a pair of curly braces.

Figure 17.32: The output of the Hello World application.

A More Useful iBasic Application

The preceding section described how you can create a simple iBasic application and publish it on the Web. Of course, the Hello World application is not very useful; the iBasic application presented next is more useful. In fact, you might want to incorporate it into your Web site.

The next application demonstrates how to create an iBasic application that uses cookies. Cookies have been implemented to make it easier for a server and client to remember previous transactions. Because HTTP is a stateless protocol, cookies are very important. The application shown in Listing 17.3 uses cookies to keep track of the last date and time a user visited a Web page. It also uses cookies to determine whether a user is visiting the Web page for the first time. If so, the iBasic application displays the Web page shown in Figure 17.33.

Figure 17.33: The Web page displayed by the iBasic application the first time a user vists the page.

If a user has already visited the Web page, the iBasic application displays a Web page similar to the one shown in Figure 17.34.

Figure 17.34: The Web page displayed by the iBasic application each subsequent time a user visits the page.

URL
Visit the following Web site for more information about client-side state HTTP cookies:
http://www.netscape.com/newsref/std/cookie_spec.html

The iBasic application in Listing 17.3 is very simple. Lines 1-7 define variables used by the iBasic application. Lines 9 -24 use cookies to determine the last day and time a user visited the Web page. Depending on whether a user has visited the Web page earlier, the Boolean variable NewUser is either set to True or False. The following application also keeps track of the host name and time someone last accessed the Web page. This information is saved in a file. Lines 26-42 process this information. Then, depending on the time of the Web server, the iBasic application displays a suitable message to the user. The remainder of the application is similar to the preceding Hello World application. Refer to the iBasic documentation for additional information about various functions used in the following code listing (Listing 17.3).


Listing 17.3. A more advanced iBasic program.

 1: {
 2: Sub Main
 3:   Dim LastLogon As Cookie,  _
 4:       NewUser   As Boolean, _
 5:       LogDate   As Variant, _
 6:       LastUser  As Variant, _
 7:       LastTime  As Variant
 8:
 9:   Set LastLogon = Cookies![Last Visit]
10:
11:   If LastLogon Is Nothing Then
12:     Set LastLogon = Cookies.Add("Last Visit")
13:     NewUser = True
14:   Else
15:     LogDate  = LastLogon
16:     NewUser  = False
17:   End If
18:
19:   With LastLogon
20:     .Value = Format(Now, "ddddd") + " at " + _
21:              Format(Now, "Long Time")
22:     .Expires = Now + 365
23:   End With
24: }
25:
26: {
27: Open "LastAccess.TMP" For Output As #1
28: Print #1, Env!REMOTE_ADDR
29: Print #1, Now
30: Close #1
31:
32: If (Dir("LastAccess") = "LastAccess" )Then
33:   Open "LastAccess" For Input As #1
34:   Line Input #1, LastUser
35:   Line Input #1, LastTime
36:   Close #1
37:   Kill "LastAccess"
38: End If
39:
40: Name "LastAccess.TMP" As "LastAccess"
41:
42: }
43:
44: <HTML>
45:
46: <BODY BGCOLOR="FFFFFF">
47:
48: <H1>
49: {If Hour(Time()) >= 18 Then}
50:   Good Evening!<P>
51: {ElseIf Hour(Time()) >= 12 Then}
52:   Good Afternoon!<P>
53: {Else}
54:   Good Morning!<P>
55: {End If}
56: </H1>
57:
58: <HR>
59:
60: Welcome to NetInnovation.COM, user from
61: %Env!REMOTE_ADDR% <BR>
62: In Maryland, USA, it is now %Format(Now, "Long Date")%
63: at %Format(Now, "Long Time")%.<P>
64:
65: {If Not NewUser Then}
66:   You last visited this Web page on %LogDate%.
67:   Thanks for coming back!<P>
68: {Else}
69:   This is your first visit to NetInnovation.COM.<BR>
70:   Thanks for giving us the opportunity to welcome
71:   you to NetInnovation.COM!<BR>
72:   We hope you enjoy this Web site and
73:   come back again, soon!<P>
74: {End If}
75:
76: This Web page was last visited on
77:   %Format(LastTime, "Long Date")% at
78:   %Format(LastTime, "Long Time")%<BR>
79:   by a user from %LastUser%.
80:
81: </BODY>
82: </HTML>
83: {End Sub}

Where to Go from Here

The preceding sections were meant to be a brief introduction to iBasic and some of its
capabilities. As you will learn when you go over iBasic documentation, iBasic is a powerful scripting language that you can use for a variety of tasks. Take some time to go over iBasic documentation and learn some of its capabilities. You can use iBasic, for example, to access powerful Win32 API functions and create sophisticated Web pages.

Summary

PolyForm and iBasic are two useful CGI applications that you can use to create interactive and dynamic Web pages. Although these two applications are not distributed free of charge or included with Windows NT, they can potentially save days or even weeks of CGI application development time. I recommended that you download both these applications and use them to make your Web site dynamic and interactive.

What's Next?

The next chapter discusses how you can publish databases on the WWW. Several powerful database publishing applications are available for Windows NT. You can use these applications to publish databases effortlessly on the Web in a matter of minutes. The next chapter also demonstrates how you can make information in a database available to users browsing a Web site.