Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CGI programming in VBScript / JScript (Windows Script Host (WSH)) #33

Open
shyangs opened this issue Aug 14, 2023 · 0 comments
Open

CGI programming in VBScript / JScript (Windows Script Host (WSH)) #33

shyangs opened this issue Aug 14, 2023 · 0 comments

Comments

@shyangs
Copy link
Owner

shyangs commented Aug 14, 2023

CGI programming in VBScript / JScript (Windows Script Host (WSH))

環境準備:假設開發環境已建立好了。參閱 #24 XAMPP 安裝與設定 (PHP, Apache, MariaDB, phpMyAdmin)。

VBScript

  1. 在目錄 C:\xampp\cgi-bin\ 新增一腳本 test1.vbs ,該腳本內容如下:
'!c:\WINDOWS\system32\cscript.exe /nologo
WScript.Echo "Content-type: text/html" & vbcrlf ' vbCrLf 是 Visual Basic中的一個字串常數,即「 Chr(13) & Chr(10) 」(回車字元與換行字元連接在一起 \n\r ),是換行的意思. 

'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>"

WScript.StdOut.WriteLine "<h2>Microsoft VBScript engines version</h2>"
WScript.StdOut.WriteLine ( _ 
	Join(Array( _ 
		ScriptEngine, ScriptEngineMajorVersion, ScriptEngineMinorVersion, ScriptEngineBuildVersion _ 
	), ".") _ 
)
  1. 在瀏覽器輸入網址 http://localhost/cgi-bin/test1.vbs。如下圖,印出 Hello world 和 VBScript 引擎版本。
    WSH_VBScript_test1 vbs

JScript

  1. 在目錄 C:\xampp\cgi-bin\ 新增一腳本 printenv.wsf ,該腳本內容如下:
'!c:/windows/system32/cscript -nologo

'https://github.com/apache/httpd/blob/trunk/docs/cgi-examples/printenv.wsf 

' To permit this cgi, replace ' on the first line above with the
' appropriate shebang, f.e. '!c:/windows/system32/cscript -nologo
'
' ***** !!! WARNING !!! *****
' This script echoes the server environment variables and therefore
' leaks information - so NEVER use it in a live server environment!
' It is provided only for testing purpose.
' Also note that it is subject to cross site scripting attacks on
' MS IE and any other browser which fails to honor RFC2616. 

''
''  printenv -- demo CGI program which just prints its environment
''

<job>
<script language="JScript">

WScript.Echo("Content-type: text/html; charset=utf-8");
WScript.Echo("\n");

WScript.Echo("Hello World!");

WScript.StdOut.WriteLine("<h2>Microsoft JScript engine version</h2>")
WScript.StdOut.WriteLine([
	ScriptEngine(), ScriptEngineMajorVersion(), ScriptEngineMinorVersion(), ScriptEngineBuildVersion()
].join('.'));

WScript.Echo("<h2>server environment variables (&#20282;&#26381;&#22120;&#29872;&#22659;&#35722;&#25976;)</h2>");

var objShell = new ActiveXObject("WScript.Shell");
var objArray = new Array();
var e = new Enumerator(objShell.Environment("PROCESS"));
for (;!e.atEnd();e.moveNext()) {
  var i = e.item().indexOf("=");
  var envvar = e.item().substring(0, i);
  var envval = e.item().substring(i + 1, e.item().length);
  envval = envval.replace("\n", "\\n");
  objArray.push(envvar + "=\"" + envval + "\"");
}
objArray.sort();
WScript.Echo(objArray.join("<br/>"));
</script>
</job>
  1. 在瀏覽器輸入網址 http://localhost/cgi-bin/printenv.wsf。如下圖,印出 Hello world 、 JScript 引擎版本和伺服器環境變數。
    WSH_JScript_printenv wsf

參考資料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant