Jump to content
The Uniform Server Community

How to run VBScripts as CGI on Apache 2.2.18


Ric
 Share

Recommended Posts

Reading the change log for Apache 2.2.18 I noticed the following statement:

mod_win32: Added shebang check for '! so that .vbs scripts work as CGI.

Win32's cscript interpreter can only use a single quote as comment char.

[Guenter Knauf]

Not very informative and no examples hence this quick guide shows how to run VBScripts as CGI on Apache 2.2.18.

 

Example 1

Create a new text file named test1.vbs with the following content:

'!c:\WINDOWS\system32\cscript.exe /nologo
wscript.echo "Content-type: text/html" & vbcrlf

'Above two lines are required do not change
'Some basic html follows:

wscript.echo "<h1>Hello World</h1>" 
wscript.echo "<p>This is output from VBScript</p>"

Save to folder UniServer\cgi-bin

 

Starts servers.

Type the following into your browser:

http://localhost/cgi-bin/test1.vbs

 

Noting spectacular displays hello world, which shows the page, has been processed as a cgi script.

 

Note: The shebang is '! followed by path to executable.

The /nologo prevents cscript displaying a banner otherwise a malformed header error is produced.

Next line is obligatory and sets the header information "Content-type" for the web page

 

Example 2

This example shows how to post data from a form and process it using a CGI VBScript.

 

First create a new page test.html with the following content:

<html>
<head>
<title>Uniform Server test</title>
</head>
<body>
<form action=/cgi-bin/test2.vbs method=post>
 Enter your name: <input name=name><br>
 Enter your age:<input name=age><br>
 Enter your phone number:<input name=phone_number><br>
 <input type=submit value="Send">
</form>
</body>
</html>

Save to folder UniServer\www

 

Now create a new text file named test2.vbs with the following content:

 

'!c:\WINDOWS\system32\cscript.exe /nologo
wscript.echo "Content-type: text/html" & vbcrlf

'Above two lines are required do not change
'Some basic html follows:

wscript.echo "<h1>Hello World</h1>" 
wscript.echo "<p>This is output from VBScript</p>"

'=== Get data sent to page
Set Shell = CreateObject("WScript.Shell")						   'Create shell
ReqMethod=Ucase(Shell.ExpandEnvironmentStrings("%REQUEST_METHOD%")) 'Get request method

Select Case ReqMethod											   'From ReqMethod
 Case "GET"														'if GET
cgi_string = cgi.ExpandEnvironmentStrings("%QUERY_STRING%")	 'expand and save cgi string
 Case "POST"													   'if POST
cgi_string = WScript.Stdin.ReadAll							  'read all from standard in 
End Select														  'save cgi string

'=== Clean data. Replace + with space and replace %2C with comma
cgi_string = replace (cgi_string, "+", " ")  
cgi_string = replace (cgi_string, "%2C", ",") 

'=== Create a dictionary (hash table)
cgi_array	 = split (cgi_string, "&")			  'Create array by spliting at & 
Set cgi_hash  = CreateObject("Scripting.Dictionary") 'Create dictionary

for i = 0 to ubound(cgi_array)					   'Scan cgi array
 cgi_var = split(cgi_array(i), "=")				 'Split into name (key) data pair
 cgi_hash.add cgi_var(0), cgi_var(1)				'Populate dictionary key/data
next

'=== Example how to use dictionary
wscript.echo "<br>"
wscript.echo "Your name = " & cgi_hash.item("name") & "<br>"
wscript.echo "Your age = " & cgi_hash.item("age") & "<br>"
wscript.echo "Your phone number = " & cgi_hash.item("phone_number") & "<br>"

Save to folder UniServer\cgi-bin

 

Starts servers.

Type the following into your browser:

http://localhost/test.html

 

Fill in the form and click send

 

Above provides working examples to get you up and running.

All the best Ric B)

Link to comment
Share on other sites

  • 2 months later...

Hi Ric I think I've come accross a similar problem with javascript. The div changed by mysql_query dosen't respond to the external.js even though I placed echo "<link relative " at the bottom of the page. I had also taken the link out of the index.html header and had it at the bottom of the page just before /body. I guess that php is a head/header responce language anyway. Could you post the header for javascript?

Link to comment
Share on other sites

  • 2 months later...

Since my original post I have received requests for a similar write up using JavaScript.

 

For anyone interested in writing CGI scripts using VBScript or JavaScript, a new tutorial has been published

on the Wiki.

 

Tutorial topics covered:

VBScript CGI

VBScript Banner Footer

URL encoding

JavaScript CGI

JavaScript Banner Footer

Get CGI variables

VBScript dictionary - database

VBScript Classes

CGI Property Class 1

CGI Property Class 2

File DB Class

File DB Class Summary

URL rewriting

 

Tutorial examples can be run on Uniform Server with Apache versions starting from 2.2.18 and above.

Alternatively download and run the new Coral-Mini Server (Uses Apache 2.2.21) it includes an html copy of

the above tutorial.

 

 

Links:

Wiki tutorial

Download Coral-Mini Server

 

Have fun

All the best

Ric ;)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...