9.10 Writing CGI Scripts for a Web Server

WSH scripts have the potential to produce simple, yet quite capable CGI (Common Gateway Interface) applications for use with web servers: programs that are run by web-server software to generate dynamic web content. For example, CGI programs can be used to process data entered in web-based fill-out forms or to read data from files and produce web content on the fly. Although a full discussion of web server implementation and CGI programming is beyond the scope of this book, there are some extra steps and additional commands necessary to accomplish write CGI programs with WSH scripts.

The first step is to set up your web server software to execute WSH scripts. There are a variety of different web server software packages (such as IIS, included with Windows XP, and Apache, freely available at http://www.apache.org), and naturally the configuration varies with each package. The following procedure shows how to set up IIS and configure it to execute WSH scripts as CGI programs.

  1. If IIS is not currently installed, go to Add or Remove Programs in Control Panel, and click Add/Remove Windows Components. Highlight Internet Information Services (IIS) from the list, and click Details. Place a checkmark next to Common Files, Internet Information Services Snap-In, World Wide Web Service, and any other components you want installed. Click OK and then click Next to complete the wizard.

  2. Start the IIS Snap-In (\Windows\system32\inetsrv\iis.msc), and then expand the branches to Internet Information Services\My Computer\Web Sites\Default Web Site. The files and folders that make up your web site are shown here (note that your setup may be different).

  3. Scripts to be executed cannot be placed in ordinary folders; otherwise, the web server will simply display their contents instead of running them. So, they must be placed in a virtual directory with executable permissions; if you've already set up such a folder, you can continue to the next step. Otherwise, go to Action figs/u2192.gif New figs/u2192.gif Virtual Directory, and follow the prompts. The Alias option is the folder name that appears in the URL when referencing the script from a browser (described subsequently), and the Directory option is the full path of the physical folder on your hard disk containing your script. Finally, when asked about Access Permissions, make sure to turn on the Execute option.

  4. Once you have a virtual directory configured, right-click the folder, click Properties, choose the Virtual Directory tab, and then click Configuration.

 

For a CGI program to work, its output must be sent to the "console," a text-based display which works like the Command Prompt. For this reason, the CScript.exe script interpreter (engine), mentioned earlier in this chapter, must be used instead of the standard WScript.exe Windows-based interpreter.

 

  1. Click Add, and type the following:

    c:\windows\system32\cscript.exe "%s" "%s"

    in the Executable field (change the path to match your system, if necessary), and type .vbs in the Extension field (make sure to include the dot).

    Naturally, the filename extension will be different for JavaScript or Perl script files. Or, if you like, you can even make up a new filename extension for use with your VBScript CGI scripts (such as .vbsc or .vbcgi), as long as what you type doesn't conflict with another entry in the list.

  2. The All Verbs, Script engine, and Check that file exists options should all be selected. Click OK, and then OK again when you're done.

The next step is to write a CGI script and place it in your executable folder. CGI scripts can use any of the commands and routines discussed elsewhere in this chapter, except, of course, for those that create dialog windows, such as MsgBox and InputBox.

The key to a CGI script, though, is the WScript.Echo command, which is used to send your text output to the web server. Here's an example of a simple four-line script that generates a basic HTML-formatted[4] web page:

[4] A discussion of HTML (HyperText Markup Language) is beyond the scope of this book, but there are many adequate HTML references on the web.

WScript.Echo "<html>" WScript.Echo "<body>" WScript.Echo "<h1>Here Comes the Metric System!</h1>" WScript.Echo "<body></html>"

To run the script, first save it in the executable folder you configured earlier. If the IISAdmin service is not currently running, start it now (via Services.msc). Then, open a web browser, and type this URL into the address bar:

http://localhost/foldername/script.vbs

where foldername is the Alias you chose for the executable folder, and script.vbs is the filename of the script. If all goes well, you should see our message, "Here Comes the Metric System!" right in the browser window. If it doesn't work, check the permissions of the script file and executable folder (right-click, select Properties, and choose the Security tab). See Chapter 8 for more information on user accounts, ownership, and file permissions.

Since we are talking about a web server, you can just as easily call the script from a remote computer, as long as your connected to a network or to the Internet, and you know the IP address or URL of your machine (visit http://www.annoyances.org/ip to find your computer's IP address). For example, if your IP address is 207.46.230.218, you'd simply type http://207.46.230.218/foldername/script.vbs.

Naturally, you'll probably want to generate dynamic (rather than static) content with your CGI script. Here's a script that displays the current date and time in the browser window:

WScript.Echo "<html><body>" WScript.Echo "Today's date is: " & Date WScript.Echo "and the current time is: " & Time WScript.Echo "<body></html>"

 

For those familiar with writing CGI programs, you may be confused by the handling of any HTTP headers you include in your WSH CGI scripts. Although the CGI specification requires that a CGI program produce its own HTTP headers (such as "Content-type: text/html"), IIS 5.x automatically generates the headers, based on the type of content it thinks you're sending (text/html for HTML or text/plain for plain text, for example). This not only means that any headers you include (with WScript.Echo) will simply appear as part of the generated page, but that there's no way to choose include your own headers.

 

If you need to obtain the value of a browser environment variable in your script, include this function:

Function Environment(EnviroName)   Set WshShell = Wscript.CreateObject("Wscript.Shell")   Set EnvHandle = WshShell.Environment("Process")   Environment = EnvHandle(EnviroName) End Function

For example, you display the user's web browser version with this short script:

WScript.Echo "Your browser's signature is:" WScript.Echo Environment("HTTP_USER_AGENT")

Some other useful environment variables include QUERY_STRING (for retrieving form input or any text after a question mark in the URL) and HTTP_COOKIE (for reading HTTP cookies).

You can, of course, use other routines in your CGI scripts. For example, here's a script that displays the contents of a text file, using the ReadFromFile function (see Section 9.4 earlier in this chapter):

OrderNum = "234323" WScript.Echo "Here is your order (number " & OrderNum & "):" WScript.Echo "<p>" WScript.Echo "<img src=""/pictures/smiley.jpg""><br>" WScript.Echo ReadFromFile("d:\data\orders\" & OrderNum & ".txt")

Note the use of Hypertext Markup Language (HTML) to include an image in the output. Although many HTML tags require quotation marks, adding a quotation mark in the middle of a line would cause WSH to confuse it with the beginning and trailing quotes. To tell VBScript to treat a quotation mark as a character to print, just put two of them together (as shown on the "smiley" line).



Windows XP Annoyances
Fixing Windows XP Annoyances
ISBN: 0596100531
EAN: 2147483647
Year: 2005
Pages: 78
Authors: David A. Karp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net