Godaddy主机ASP读写Cookie的方法


Function BinaryToString(binData, CharSet)
Dim objStream
Dim strTmp
Const adTypeBinary = 1
Const adTypeText = 2
Const adModeReadWrite = 3
' init stream
Set objStream = Server.CreateObject("ADODB.Stream")
If CharSet<>"" Then
objStream.CharSet = CharSet
Else
Err.Raise vbObjectError + 1, "IllegalArgument", "Charset is required"
End If
objStream.Mode = adModeReadWrite
objStream.Type = adTypeBinary
objStream.Open
' write bytes into stream
objStream.Write binData
objStream.Flush
' rewind stream and read text
objStream.Position = 0
objStream.Type = adTypeText
strTmp = objStream.ReadText
' close up and return
objStream.Close
BinaryToString = strTmp

Set objStream = Nothing
End Function

Function convertCharSet(text, origCharset, newCharSet)
Dim binaryData
binaryData = StringToBinary(text, origCharset)
convertCharSet = BinaryToString(binaryData, newCharSet)
End Function

Function readCookies(cookie)
Dim cookieValue
cookieValue = Request.Cookies(cookie)
if "" = cookieValue then
readCookies = ""
else
readCookies = convertCharSet(cookieValue, "x-ansi", "utf-8")
end if
End Function

Function writeCookies(cookie, cookieValue)
dim asciiString
If cookieValue <> "" Then
asciiString = convertCharSet(cookieValue, "utf-8", "x-ansi")
Else
asciiString = ""
End If
Response.Cookies(cookie) = asciiString
If Request.ServerVariables("SERVER_NAME") = "你的主站" Then
Response.Cookies(cookie).Path = "/"
Else
Response.Cookies(cookie).Path = ""
End If
Response.Cookies(cookie).Expires=Date+60 '自己设置时长
writeCookies = asciiString
End Function

打赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注