utf-8 asp页面如何共享gb2312编码页面cookie
在gb2312编码asp页面生成的cookie,编码是用gb2312编码的,这样在utf-8的页面中获取到的内容是通过gb2312编码的,会得到乱码。
解决办法就是cookie内容存储用utf-8编码过的字符串,这样在utf-8编码的页面获取到这些utf-8编码后,解码还原成内容就行了。
小提示:gb2312的asp页面coolie存储gb2312编码过的字符串也行,但是在utf-8页面解码gb2312编码内容比较麻烦,需要一个gb2312编码的中间页面,具体可以参考这篇文章:asp gb2312/utf-8编码和解码
测试代码
gb2312.asp,负责生成utf-8编码的cookie和解码utf-8编码的cookie
<%@ language="vbscript" codepage="936" %> <% dim iSourceCode'网站默认编码 iSourceCode=Session.CodePage'得到当前页面指定的编码 'sStr:要编码的字符串 'iTargetCode:指定的编码 Function UrlEncode(sStr,iTargetCode)'将内容转换为指定编码的字符串 If iTargetCode <> iSourceCode Then '如果指定要编码的类型与当前页面编码不一至,则临时设置处理该函数时的页面编码类型 Session.CodePage = iTargetCode UrlEncode = Server.UrlEncode(sStr) Session.CodePage =iSourceCode Else UrlEncode = Server.UrlEncode(sStr) End If End Function '================================================== '将utf-8编码的字符串解码为对应的中文 '此方法可以在gb2312或者utf-8编码的页面来解码utf-8编码的字符串 '================================================== Function UTF8URLDecode(ByVal strIn) UTF8URLDecode = "" Dim sl: sl = 1 Dim tl: tl = 1 Dim key: key = "%" Dim kl: kl = Len(key) Dim hh, hi, hl Dim a sl = InStr(sl, strIn, key, 1) Do While sl>0 If (tl=1 And sl<>1) or tl<sl Then UTF8URLDecode = UTF8URLDecode & Mid(strIn, tl, sl-tl) Select Case UCase(Mid(strIn, sl+kl, 1)) Case "U":'Unicode URLEncode a = Mid(strIn, sl+kl+1, 4) UTF8URLDecode = UTF8URLDecode & ChrW("&H" & a) sl = sl + 6 Case "E":'UTF-8 URLEncode hh = Mid(strIn, sl+kl, 2) a = Int("&H" & hh)'ascii码 If Abs(a)<128 Then sl = sl + 3 UTF8URLDecode = UTF8URLDecode & Chr(a) Else hi = Mid(strIn, sl+3+kl, 2) hl = Mid(strIn, sl+6+kl, 2) a = ("&H" & hh And &H0F) * 2 ^12 or ("&H" & hi And &H3F) * 2 ^ 6 or ("&H" & hl And &H3F) If a<0 Then a = a + 65536 UTF8URLDecode = UTF8URLDecode & ChrW(a) sl = sl + 9 End If Case Else:'Asc URLEncode hh = Mid(strIn, sl+kl, 2)'高位 a = Int("&H" & hh)'ascii码 If Abs(a)<128 Then sl = sl + 3 Else hi = Mid(strIn, sl+3+kl, 2)'低位 a = Int("&H" & hh & hi)'非ascii码 sl = sl + 6 End If UTF8URLDecode = UTF8URLDecode & Chr(a) End Select tl = sl sl = InStr(sl, strIn, key, 1) Loop UTF8URLDecode = UTF8URLDecode & Mid(strIn, tl) End Function v=request.Cookies("utf8") if v="" or isnull(v) then v="web开发网" v=UrlEncode(v,65001)'将内容转换为utf-8编码,这样在utf-8编码页面好解码 response.Cookies("utf8").expires=date+365 response.Cookies("utf8").domain="顶级域名" response.Cookies("utf8").path="/" response.Cookies("utf8")=v response.Write "cookie utf8|"&v else response.Write "cookie utf8|“"&v&"”解码结果<br/>"&UTF8URLDecode(v) end if %>
utf-8.asp,负责解码gb2312.asp生成的utf-8编码的cookie
<%@ language="vbscript" codepage="65001" %> <% '================================================== '将utf-8编码的字符串解码为对应的中文 '此方法可以在gb2312或者utf-8编码的页面来解码utf-8编码的字符串 '================================================== Function UTF8URLDecode(ByVal strIn) UTF8URLDecode = "" Dim sl: sl = 1 Dim tl: tl = 1 Dim key: key = "%" Dim kl: kl = Len(key) Dim hh, hi, hl Dim a sl = InStr(sl, strIn, key, 1) Do While sl>0 If (tl=1 And sl<>1) or tl<sl Then UTF8URLDecode = UTF8URLDecode & Mid(strIn, tl, sl-tl) Select Case UCase(Mid(strIn, sl+kl, 1)) Case "U":'Unicode URLEncode a = Mid(strIn, sl+kl+1, 4) UTF8URLDecode = UTF8URLDecode & ChrW("&H" & a) sl = sl + 6 Case "E":'UTF-8 URLEncode hh = Mid(strIn, sl+kl, 2) a = Int("&H" & hh)'ascii码 If Abs(a)<128 Then sl = sl + 3 UTF8URLDecode = UTF8URLDecode & Chr(a) Else hi = Mid(strIn, sl+3+kl, 2) hl = Mid(strIn, sl+6+kl, 2) a = ("&H" & hh And &H0F) * 2 ^12 or ("&H" & hi And &H3F) * 2 ^ 6 or ("&H" & hl And &H3F) If a<0 Then a = a + 65536 UTF8URLDecode = UTF8URLDecode & ChrW(a) sl = sl + 9 End If Case Else:'Asc URLEncode hh = Mid(strIn, sl+kl, 2)'高位 a = Int("&H" & hh)'ascii码 If Abs(a)<128 Then sl = sl + 3 Else hi = Mid(strIn, sl+3+kl, 2)'低位 a = Int("&H" & hh & hi)'非ascii码 sl = sl + 6 End If UTF8URLDecode = UTF8URLDecode & Chr(a) End Select tl = sl sl = InStr(sl, strIn, key, 1) Loop UTF8URLDecode = UTF8URLDecode & Mid(strIn, tl) End Function v=request.Cookies("utf8") if v<>"" then response.Write "cookie utf8|“"&v&"”解码结果<br/>"&UTF8URLDecode(v) end if %>
加支付宝好友偷能量挖...
原创文章,转载请注明出处:utf-8 asp页面如何共享gb2312编码页面cookie