|
Free Open Book
Windows XP Annoyances |
9.10 Writing CGI Scripts for a Web ServerWSH 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.
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:
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>"
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). |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |