487
edits
(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...") |
(Punctuation and grammatical changes; some clarification.) |
||
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 VBScrpits 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 | '''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 | 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 | 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 is 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 is obligatory otherwise again a malformed header error is 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 | The second '''vbLF''' produces a blank line. This is required before any content is output. | ||
===Complete test script=== | ===Complete test script=== | ||
Line 96: | Line 96: | ||
|} | |} | ||
===Test Script 2=== | ===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. | |||
{| | {| | ||
|- | |- | ||
Line 131: | Line 130: | ||
|} | |} | ||
===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. | |||
* Create a new file test3.vbs with content as shown on right. | * Create a new file test3.vbs with content as shown on right. | ||
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 major changes in output. | ||
|style="background:#f5f5f5;"| | |style="background:#f5f5f5;"| | ||
Line 178: | Line 176: | ||
|} | |} | ||
==Summary== | ==Summary== | ||
Every CGI script must contain a '''shebang''' first line of any script | 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: | |||
<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- | 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> | ||
==Where to next== | ==Where to next== | ||
[[CGI: VBScript Banner Footer| | The [[CGI: VBScript Banner Footer|next page]] covers adding a banner and footer using include files. | ||
---- | ---- |