用VB编写标准CGI程序
作者: 来源: 添加时间:2006-5-21 20:58:20Print #1, "" & vbCrLf
Print #1, "留言时间:" & Date$ & " " & Time$ & vbCrLf
Print #1, "姓名: " & sName & vbCrLf
If Len(sEmail) <> 0 Then
Print #1, "E-mail: " & sEmail & "" & vbCrLf
End If
If Len(sURL) <> 0 Then
Print #1, "我的主页: " & sURL & "" & vbCrLf
End If
If Len(sfrom) <> 0 Then
Print #1, "我来自: " & sfrom & vbCrLf
End If
Print #1, "我的建议: " & vbCrLf
Print #1, sComment & vbCrLf
Print #1, "" & vbCrLf
Do ’本循环体用于将留言簿剩余的东西写入留言簿
Line Input #2, tempstring
Print #1, tempstring
Loop While Not EOF(2)
Close #1
Close #2
Kill guestbook ’删除旧的留言簿
Name tempFileName As guestbook ’将临时文件改成新的留言簿
OutPut "非常感谢您的留言!" & chinesetail
OutPut "欢迎您经常光顾本主页!" & chinesetail
OutPut ""
End Sub
Sub OutPut(s As String) ’ 本子程序用于向标准输出写信息
Dim lBytesWritten As Long
s = s & vbCrLf
WriteFile hStdOut, s, Len(s), lBytesWritten, ByVal 0&
End Sub
Public Function GetCgiValue(cgiName As String) As String
’ 本子程序可以获取表单上某一元素的数据
Dim delim2 As Long ’ position of "="
Dim delim1 As Long ’ position of "&"
Dim n As Integer
Dim pointer1 As Long,pointer2 As Long,length As Long,length1 As Long
Dim tmpstring1 As String,tmpstring2 As String
pointer1 = 1
pointer2 = 1
delim2 = InStr(pointer2, sFormData, "=")
pointer2 = delim2 + 1
Do
length = delim2 - pointer1
tmpstring1 = Mid(sFormData, pointer1, length)
delim1 = InStr(pointer1, sFormData, "&")
pointer1 = delim1 + 1
length1 = delim1 - pointer2
If delim1 = 0 Then length1 = lContentLength + 1 - pointer2
If tmpstring1 = cgiName Then
tmpstring2 = Mid$(sFormData, pointer2, length1)
GetCgiValue = UrlDecode(tmpstring2)
Exit Do
End If
If delim1 = 0 Then
Exit Do
End If
delim2 = InStr(pointer2, sFormData, "=")
pointer2 = delim2 + 1
Loop
End Function
Public Function UrlDecode(ByVal sEncoded As String) As String
’ 本函数可以对用户输入的数据进行URL解码
Dim pointer As Long ’ sEncoded position pointer
Dim pos As Long ’ position of InStr target
Dim temp As String
If sEncoded = "" Then Exit Function
pointer = 1
Do ’本循环体用于将"+"转换成空格
pos = InStr(pointer, sEncoded, "+")
If pos = 0 Then Exit Do
Mid$(sEncoded, pos, 1) = " "
pointer = pos + 1
Loop
pointer = 1
Do
’本循环体用于将%XX转换成字符。对于两个连续的%XX,如果第一个%XX 不是某些特指的Web系统保留字符,将把它们转换成汉字
pos = InStr(pointer, sEncoded, "%")
If pos = 0 Then Exit Do
temp = Chr$("&H" & (Mid$(sEncoded, pos + 1, 2)))
If Mid(sEncoded, pos + 3, 1) = "%" And (temp <> ":") And (temp <> "/") _
And (temp <> "(") And (temp <> ")") And (temp <> ".") And (temp <> ",") _
And (temp <> ";") And (temp <> "%") Then
Mid$(sEncoded, pos, 2) = Chr$("&H" & (Mid$(sEncoded, pos + 1, 2)) _
& (Mid$(sEncoded, pos + 4, 2)))
sEncoded = Left$(sEncoded, pos) & Mid$(sEncoded, pos + 6)
pointer = pos + 1
Else
Mid$(sEncoded, pos, 1) = temp
sEncoded = Left$(sEncoded, pos) & Mid$(sEncoded, pos + 3)
pointer = pos + 1
End If
Loop
UrlDecode = sEncoded
Exit Function
End Function
Public Function TempFile(sPath As String, sPrefix As String) As String
’本函数可以获得一个唯一的临时文件名
Dim x As Long,rc As Long
TempFile = String(127, Chr$(0))
rc = GetTempFileName(sPath, sPrefix, ByVal 0&, TempFile)
x = InStr(TempFile, Chr$(0))
If x > 0 Then TempFile = Left$(TempFile, x - 1)
End Function
虽然目前已经有很多可以取代CGI且其性能较CGI要高的技术(例如ASP、ISAPI、NSAPI等),但使用它们时需要用到专门的知识和工具,并且利用这些技术所编制的程序只适用于特定的Web服务器或系统平台。考虑到CGI编程具有易用易学性、跨服务器平台特性等优点,因此,CGI程序还将在WWW上占有一席之地。
站内搜索