原始碼 我們都知道asp這一類的伺服器端處理的程式,其好處之一就是只向用戶端輸出
標準的Html流。因此可以起到向客戶隱藏細節的作用。也就是說當我們在瀏覽器中
鍵入asp程式的網址後只能看見標準的Html檔案,而不能看見asp的內容。但有時,
例如在一個asp的教學網站,我們有必要顯示asp檔案的內容,或者你願意將你的原
代碼與人享,通過一個程式將代碼顯示出來。
下面是我編寫的一個asp程式,view_code.asp,它提供兩種提交方式:一種是用
表格提交,即你知道了該源檔案的物理地址(類似於:c:\asp_source\test.asp的
形式)。一種是採用get方式提交(類似於:<a href="view_code.asp?code_path=
<%=server.mappath(request.servervariables("PATH_INFO"))%>&cgi_type=asp">
點擊此處查看原代碼</a>)。另外它還支援兩種cgi指令碼,一種是asp,一種是php。
程式碼片段:
<%
on error resume next
'忽略程式執行中的錯誤,在程式的最後統一處理。
%>
<%
function rt_min(num1,num2)
'該子程式用於返回兩數中不等於零的最小數。
if num1=0 and num2=0 then
rt_min=-1
elseif num1=0 then
rt_min=num2
elseif num2=0 then
rt_min=num1
elseif num1<num2 then
rt_min=num1
else
rt_min=num2
end if
end function
%>
<%
function line_check(strline,cgi_type)
'該子程式用於檢查輸入段中是否包含有"<%、%>、<script或</script的特殊字元
dim cgi_flag
if cgi_type="php" then
cgi_flag="?"
else
cgi_flag="%"
end if
'定義的cgi_flag用於代表php和asp的不同標識符
line_check=0
itemp=0
ipos=instr(strline,"<"&cgi_flag)
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=1
end if
ipos=instr(strline,cgi_flag&">")
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=2
end if
ipos=instr(1,strline,"<"&"script",1)
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=3
end if
ipos=instr(1,strline,"<"&"/script",1)
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=4
end if
end function
%>
<%
sub printhtml(strline)
'該子過程用於列印不含有上述四種特殊標記的行
ispace=len(strline)-len(ltrim(strline))
i=1
while(mid(strline,i,1))=chr(9)
ispace=ispace+5
i=i+1
wend
'統計空白的數量
if ispace>0 then
for i=1 to ispace
response.write(" ")
next
end if
ipos=instr(strline,"<")
if ipos then
response.write(left(strline,ipos-1))
response.write("<")
'用<來替代<,使瀏覽器不解釋<>中的標記
strline=right(strline,len(strline)-ipos)
call printhtml(strline)
'自調用,直到沒有<的出現
else
response.write(strline)
end if
end sub
%>
<%
sub printline(strline,iflag,cgi_type)
'該自過程用於根據line_check的傳回值分別處理
dim cgi_flag
if cgi_type="php" then
cgi_flag="?"
else
cgi_flag="%"
end if
select case iflag
case 0
call printhtml(strline)
case 1
ipos=instr(strline,"<"&cgi_flag)
call printhtml(left(strline,ipos-1))
response.write("<font color=#ff0000>")
response.write("<"&cgi_flag)
strline=right(strline,len(strline)-ipos-1)
call printline(strline,line_check(strline,cgi_type),cgi_type)
'自調用,直到沒有四種特殊標記的出現
case 2
ipos=instr(strline,cgi_flag&">")
call printhtml(left(strline,ipos-1))
response.write(cgi_flag&">")
response.write("</font>")
strline=right(strline,len(strline)-ipos-1)
call printline(strline,line_check(strline,cgi_type),cgi_type)
case 3
ipos=instr(1,strline,"<"&"script",1)
call printhtml(left(strline,ipos-1))
response.write("<font color=#00ff00>")
response.write("<"&"script")
strline=right(strline,len(strline)-ipos-6)
call printline(strline,line_check(strline.cgi_type),cgi_type)
case 4
ipos=instr(1,strline,"<"&"/script>",1)
call printhtml(left(strline,ipos-1))
response.write("lt;"&"/script"&">")
response.write("</font>")
strline=right(strline,len(strline)-ipos-8)
call printline(strline,line_check(strline,cgi_type),cgi_type)
case else
response.write("error")
end select
end sub
%>
<html>
<head>
<title> view cgi_code(.asp or .php) </title>
</head>
<body>
<form action="view_code.asp" method="POST">
請輸入路徑:<input type=text name="code_path">
請選擇類型:<select name="cgi_type&qu