Asp解决URL中文编解码问题

今天用asp做一个外文网站,在开发到会员注册后通过邮件确认会员时出现问题,发现使用数字、字母作为会员名的都没有问题,但用中文汉字作为会员名的在点击链接激活时却发现搜索数据库找不到此用户名,但在注册时检查用户名和会员登录时都没有问题啊,检查SQL语句没有发现任何问题,反复研究才知道,通过浏览器的GET方式获取的值全为UTF8编码的,网页在获取到后出现编码问题,所以需要使用UrlEncode和UrlDecode转换方式加以解决。

解决方案如下:
在发送激活邮件前使用UrlEncode函数(此函数为ASP内部函数)给用户名加密。

代码为:
username=Server.UrlEncode(username)

激活URL链接为
activeuser.asp?userName="&username&"&userpassword="&userpass&"&checkcode="&checkcode


在验证激活页面使用UrlDecode函数(此函数需自己写)为你使用UrlEncode函数加密过的变量解密

代码为:
userName=URLDecode(userName)

这样就大功告成!

附UrlDecode函数(可以解密中文)


Function URLDecode(ByVal strIn)
URLDecode = ""
Dim sl: sl = 1
Dim tl: tl = 1
Dim key: key = "%"
Dim kl: kl = Len(key)
sl = InStr(sl, strIn, key, 1)
Do While sl>0
If (tl=1 And sl<>1) or tl<sl Then
URLDecode = URLDecode & Mid(strIn, tl, sl-tl)
End If
Dim hh, hi, hl
Dim a
Select Case UCase(Mid(strIn, sl+kl, 1))
Case "U":'Unicode URLEncode
a = Mid(strIn, sl+kl+1, 4)
URLDecode = URLDecode & 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
URLDecode = URLDecode & 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
URLDecode = URLDecode & 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
URLDecode = URLDecode & Chr(a)
End Select
tl = sl
sl = InStr(sl, strIn, key, 1)
Loop
URLDecode = URLDecode & Mid(strIn, tl)
End Function


[本日志由 cnfgg 于 2009-08-24 04:34 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: URL 函数
相关日志:
评论: 1 | 引用: 0 | 查看次数: -
回复回复hihai[2010-05-29 05:38 PM | del]
嗯,不错
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.