CGI: VBScript CGI: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
(Created page with "{{Nav CGI}} '''VBScript CGI''' This page covers running VBScrpits as CGI scripts and introduces the CGI shebang. It assumes the Apache server has been configured to permit runn...")
 
m (spelling fix)
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
'''VBScript CGI'''
'''VBScript CGI'''


This page covers running VBScrpits as CGI scripts and introduces the CGI shebang. It assumes the Apache server has been configured to permit running of CGI scripts and test folder vbs_test and .htaccess have been created as explained on the [[CGI: Introduction| previous page]].
This page covers running VBScripts as CGI scripts and introduces the CGI shebang. It assumes the Apache server has been configured to permit the running of CGI scripts, and test folder vbs_test and .htaccess have been created as explained on the [[CGI: Introduction| previous page]].


__TOC__
__TOC__
Line 9: Line 9:
==Test Script 1==
==Test Script 1==


Inside folder vbs_test create a new file '''test1.vbs''' with the following content:
Inside folder vbs_test, create a new file '''test1.vbs''' with the following content:
<pre>
<pre>
Wscript.Echo "<h1>VBScript CGI Test</h1>"
Wscript.Echo "<h1>VBScript CGI Test</h1>"
Wscript.Quit 0
Wscript.Quit 0
</pre>
</pre>
This script writes (echoes) string "<nowiki><h1>VBScript CGI Test</h1></nowiki>" to the standard output stream followed by exit code 0 (Wscript.Quit 0).
This script writes (echoes) the string "<nowiki><h1>VBScript CGI Test</h1></nowiki>" to the standard output stream followed by exit code 0 (Wscript.Quit 0).
===Run Script on server===
===Run Script on server===
Run the script as follows:
Run the script as follows:
Line 20: Line 20:
* In your browser type: '''<nowiki>http://localhost:8081/vbs_test/test1.vbs</nowiki>'''
* In your browser type: '''<nowiki>http://localhost:8081/vbs_test/test1.vbs</nowiki>'''


'''Note:''' If you are using Uniform Server the port is not required instead type the following into your browser '''<nowiki>http://localhost/vbs_test/test1.vbs</nowiki>'''
'''Note:''' If you are using The Uniform Server, the port is not required. Instead type the following into your browser: '''<nowiki>http://localhost/vbs_test/test1.vbs</nowiki>'''


{|
{|
Line 31: Line 31:
Reason script fails; Apache does not know how to locate the program that will run this script!
Reason script fails; Apache does not know how to locate the program that will run this script!


Windows uses file association to run programs; it checks the file extension and runs the appropriate program that has been assigned to that extension. Apache does not use this mechanism, CGI scripts can have any file extension for example a script written in VBScript has the extension .vbs one written in Vfred may have the extention .fred all Apache knows is the extension is assocuted with a CGI script.
Windows uses file association to appropriately run programs; it checks the file extension and runs the appropriate program that has been assigned to that extension. Apache does not use this mechanism, CGI scripts can have any file extension for example a script written in VBScript has the extension .vbs one written in Vfred may have the extension .fred ; all Apache knows is that the extension is associated with a CGI script.


In reality Apache does know where to find the executable program! Every CGI script must contain a line pointing to the program that will be used to run the script. This line is referred to as the '''shebang'''.
In reality, Apache does know where to find the executable program! Every CGI script must contain a line pointing to the program that will be used to run the script. This line is referred to as the '''shebang'''.
|
|
<pre>
<pre>
Line 55: Line 55:
|}
|}
===Add a shebang===
===Add a shebang===
A CGI VBScript requires a shebang informing Apache what program is required to run the script. It has the following format:
A CGI VBScript requires a shebang to inform Apache what program is required to run the script. It has the following format:
<pre>
<pre>
'!c:/windows/system32/cscript //nologo
'!c:/windows/system32/cscript //nologo
Line 61: Line 61:
'''Note:'''  
'''Note:'''  


The shebang is ''''!''' followed by the path to executable that will run the script in this case the Windows Script Host '''cscript.exe'''<br />
The shebang is ''''!''' followed by the path to the executable that will run the script, which in this case is the Windows Script Host '''cscript.exe'''<br />
The '''//nologo''' prevents cscript displaying a banner otherwise a malformed header error is produced.
The '''//nologo''' prevents cscript from displaying a banner, otherwise a malformed header error would be produced.


===Content-type===
===Content-type===
Following the shebang a content type header is required. This is obligatory otherwise again a malformed header error is produced
Following the shebang, a content type header is required. This also is obligatory, otherwise a malformed header error would be produced.
<pre>
<pre>
Wscript.Echo "Content-type: text/html" & vbLF & vbLF
Wscript.Echo "Content-type: text/html" & vbLF & vbLF
Line 71: Line 71:
'''Note:'''
'''Note:'''


The second '''vbLF''' produces a blank line this is required before any content is output.
The second '''vbLF''' produces a blank line. This is required before any content is output.


===Complete test script===
===Complete test script===
Line 77: Line 77:
|-
|-
|
|
* Edit file test1.vbs to have content as shown on right.
* Edit file test1.vbs to have the contents as shown on right.
* Start Apache if not already running
* Start Apache if not already running
* Enter: <nowiki>http://localhost:8081/vbs_test/test1.vbs</nowiki><br /> (or refresh browser page)
* Enter: <nowiki>http://localhost:8081/vbs_test/test1.vbs</nowiki><br /> (or refresh browser page)
Line 96: Line 96:
|}
|}


'''''[[#top | Top]]'''''
===Test Script 2===
===Test Script 2===
Above example although fine produces static content and you would be better off using a standard html page. Real reason for writing CGI scripts is to produce dynamic content. Following script expands on the above template (test1.vbs) to display current date and time.
The above example, although functional, produces only static content and you would be better off using a standard html page. The real reason for writing CGI scripts is to produce dynamic content. The following script expands on the above template (test1.vbs) to display the current date and time.
{|
{|
|-
|-
|
|
* Create a new file test2.vbs with content as shown on right.
* Create a new file test2.vbs with the contents as shown on right.
* Start Apache if not already running
* Start Apache if not already running
* Enter: <nowiki>http://localhost:8081/vbs_test/test2.vbs</nowiki>
* Enter: <nowiki>http://localhost:8081/vbs_test/test2.vbs</nowiki>
Line 120: Line 119:
|
|
The script produces output as shown on right.<br />
The script produces output as shown on right.<br />
Note: Date and time defined by your PC<br />
Note: Date and time are defined by your PC<br />
Refresh page and note the time updates (dynamic page).
Refresh page and note that the time updates (dynamic page).


|style="background:#f5f5f5;"|
|style="background:#f5f5f5;"|
Line 131: Line 130:
|}
|}


'''''[[#top | Top]]'''''
===Test Script 3===
===Test Script 3===
{|
{|
Line 137: Line 135:
|
|
Although the majority of html tags are optional you do need to<br />
Although the majority of html tags are optional you do need to<br />
include a minimum set of tags as per a standard html page<br />
include a minimum set of tags as per a standard html page.<br />
otherwise the page may break when opened in different browsers.
Otherwise the page may fail when opened in different browsers.


* Create a new file test3.vbs with content as shown on right.
* Create a new file test3.vbs with the contents as shown on right.
* Start Apache if not already running
* Start Apache if not already running
* Enter: <nowiki>http://localhost:8081/vbs_test/test3.vbs</nowiki>
* Enter: <nowiki>http://localhost:8081/vbs_test/test3.vbs</nowiki>
Line 168: Line 166:
'''Note:'''
'''Note:'''
The page title is now displayed on the browser tab and in<br />
The page title is now displayed on the browser tab and in<br />
browser title bar. There are no major changes in output.
the browser title bar. There are no other changes in output.


|style="background:#f5f5f5;"|
|style="background:#f5f5f5;"|
Line 178: Line 176:
|}
|}


'''''[[#top | Top]]'''''
 
==Summary==
==Summary==
Every CGI script must contain a '''shebang''' first line of any script this informs Apache what scripting engine to use. The page '''Content-type header''' must follow this line.
Every CGI script must contain a '''shebang''' as the first line of any script. This informs Apache what scripting engine to use. The '''Content-type header''' for the page must follow this line.


Page template shown below:
The page template is shown below:
<pre>
<pre>
'!c:/windows/system32/cscript //nologo
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF
Wscript.Echo "Content-type: text/html" & vbLF & vbLF
</pre>
</pre>
Running VBScripts on Uniform Server and Coral-mini requires an .htaccess file placed in the directory containing the scripts. This file enables CGI script execution and has the following content:
Running VBScripts on The Uniform Server and Coral-Mini requires an .htaccess file be placed in the directory containing the scripts. This file enables CGI script execution and has the following content:
<pre>
<pre>
AddHandler cgi-script .vbs  
AddHandler cgi-script .vbs  
Line 193: Line 191:
Options +FollowSymlinks
Options +FollowSymlinks
</pre>
</pre>
'''''[[#top | Top]]'''''
 
==Where to next==
==Where to next==
[[CGI: VBScript Banner Footer|Next page]] covers adding a banner and footer using include files.
The [[CGI: VBScript Banner Footer|next page]] covers adding a banner and footer using include files.
 
'''''[[#top | Top]]'''''


----
----

Latest revision as of 10:36, 12 November 2011

VBScript CGI

This page covers running VBScripts as CGI scripts and introduces the CGI shebang. It assumes the Apache server has been configured to permit the running of CGI scripts, and test folder vbs_test and .htaccess have been created as explained on the previous page.

Test Script 1

Inside folder vbs_test, create a new file test1.vbs with the following content:

Wscript.Echo "<h1>VBScript CGI Test</h1>"
Wscript.Quit 0

This script writes (echoes) the string "<h1>VBScript CGI Test</h1>" to the standard output stream followed by exit code 0 (Wscript.Quit 0).

Run Script on server

Run the script as follows:

  • Start the Apache server (double click on server_start.bat)
  • In your browser type: http://localhost:8081/vbs_test/test1.vbs

Note: If you are using The Uniform Server, the port is not required. Instead type the following into your browser: http://localhost/vbs_test/test1.vbs

The script fails

Browser message and entry in Apache log file are shown on the right.

Reason script fails; Apache does not know how to locate the program that will run this script!

Windows uses file association to appropriately run programs; it checks the file extension and runs the appropriate program that has been assigned to that extension. Apache does not use this mechanism, CGI scripts can have any file extension for example a script written in VBScript has the extension .vbs one written in Vfred may have the extension .fred ; all Apache knows is that the extension is associated with a CGI script.

In reality, Apache does know where to find the executable program! Every CGI script must contain a line pointing to the program that will be used to run the script. This line is referred to as the shebang.

### Browser ###
500 Internal Server Error:
The server encountered an internal error or misconfiguration
and was unable to complete your request.

Please contact the server administrator, admin@localhost and
inform them of the time the error occurred, and anything you
might have done that may have caused the error.

More information about this error may be available in the
server error log.

### Apache Log file ###
[Mon Aug 29 13:25:00 2011] [error] [client 127.0.0.1]
(9)Bad file descriptor: don't know how to spawn child process:
C:UniServer/www/vbs_test/test1.vbs

Add a shebang

A CGI VBScript requires a shebang to inform Apache what program is required to run the script. It has the following format:

'!c:/windows/system32/cscript //nologo

Note:

The shebang is '! followed by the path to the executable that will run the script, which in this case is the Windows Script Host cscript.exe
The //nologo prevents cscript from displaying a banner, otherwise a malformed header error would be produced.

Content-type

Following the shebang, a content type header is required. This also is obligatory, otherwise a malformed header error would be produced.

Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Note:

The second vbLF produces a blank line. This is required before any content is output.

Complete test script

  • Edit file test1.vbs to have the contents as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test1.vbs
    (or refresh browser page)
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Wscript.Echo "<h1>VBScript CGI Test 1</h1>"
Wscript.Quit 0

The script produces output as shown on right.

VBScript CGI Test 1

Test Script 2

The above example, although functional, produces only static content and you would be better off using a standard html page. The real reason for writing CGI scripts is to produce dynamic content. The following script expands on the above template (test1.vbs) to display the current date and time.

  • Create a new file test2.vbs with the contents as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test2.vbs
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Wscript.Echo "<h1>VBScript CGI Test 2</h1>"
WScript.Echo "<p>Today's date is: " & Date & "</p>"
WScript.Echo "<p>The current time is: " & Time & "</p>"
Wscript.Quit 0

The script produces output as shown on right.
Note: Date and time are defined by your PC
Refresh page and note that the time updates (dynamic page).

VBScript CGI Test 2

Today's date is: 21/09/2011

The current time is: 18:05:12

Test Script 3

Although the majority of html tags are optional you do need to
include a minimum set of tags as per a standard html page.
Otherwise the page may fail when opened in different browsers.

  • Create a new file test3.vbs with the contents as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test3.vbs
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

WScript.Echo "<html>"
WScript.Echo "<title>Test 3</title>"
WScript.Echo "<body>"

Wscript.Echo "<h1>VBScript CGI Test 3</h1>"
WScript.Echo "<p>Today's date is: " & Date & "</p>"
WScript.Echo "<p>The current time is: " & Time & "</p>"

WScript.Echo "</body>"
WScript.Echo "</html>"

Wscript.Quit 0

The script produces output as shown on right.

Note: The page title is now displayed on the browser tab and in
the browser title bar. There are no other changes in output.

VBScript CGI Test 3

Today's date is: 21/09/2011

The current time is: 18:05:12


Summary

Every CGI script must contain a shebang as the first line of any script. This informs Apache what scripting engine to use. The Content-type header for the page must follow this line.

The page template is shown below:

'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Running VBScripts on The Uniform Server and Coral-Mini requires an .htaccess file be placed in the directory containing the scripts. This file enables CGI script execution and has the following content:

AddHandler cgi-script .vbs 
Options +ExecCGI
Options +FollowSymlinks

Where to next

The next page covers adding a banner and footer using include files.