Jump to content


Photo

How to run VBScripts as CGI on Apache 2.2.18


  • Please log in to reply
2 replies to this topic

#1 Ric

Ric

    Project Manager

  • Admin
  • PipPipPipPipPip
  • 1,535 posts
  • Gender:Male
  • Location:Cambridge,UK
  • Interests:Electronics
    Internet in general
    Open source projects
  • SourceForge IDmpgcan
  • Wiki ID: Ric
  • Main OS: Windows XP

Posted 13 May 2011 - 06:37 AM

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)

#2 Heinz Stapff

Heinz Stapff

    Member

  • Member
  • PipPip
  • 80 posts
  • Gender:Male
  • Location:Riberas Del Pilar, Chapala, Jalisco, Mexico
  • Interests:Architecture, AutoCAD,
  • Main OS: Windows 8

Posted 12 August 2011 - 11:13 AM

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?

#3 Ric

Ric

    Project Manager

  • Admin
  • PipPipPipPipPip
  • 1,535 posts
  • Gender:Male
  • Location:Cambridge,UK
  • Interests:Electronics
    Internet in general
    Open source projects
  • SourceForge IDmpgcan
  • Wiki ID: Ric
  • Main OS: Windows XP

Posted 07 November 2011 - 06:32 PM

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 ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users