解析动网用户登录信息的保存格式

平时我们在站点上保存用户成功登录信息的方式一般都是形如Session("username")="XXX"、Session("login")=yes,而动网的保存方式却有点特别,它也是将信息保存于Session对象中,但格式却是XML片断,如:
引用内容 引用内容
<xml><userinfo userid="1" username="admin" userpassword="a0b923820dcc509a" useremail="eway@aspsky.net" userpost="0" usertopic="0" usersex="0" userface="images/userface/image1.gif" userwidth="32" userheight="32" joindate="2003-12-30 下午 04:34:32" lastlogin="2006-6-15 上午 02:04:05" cometime="2006-6-15 上午 02:08:19" activetime="2006-6-15 上午 01:08:19" userlogins="22" lockuser="0" userclass="管理员" usergroupid="1" usergroup="" userwealth="196" userep="132" usercp="66" userpower="0" userbirthday="" userlastip="127.0.0.1" userdel="0" userisbest="0" userhidden="2" usermsg="" ischallenge="0" usermobile="" titlepic="level20.gif" usertitle="" truepassword="5738498LLG536NcG" usertoday="0|0|0|0|0" usermoney="0" userticket="0" followmsgid="" vip_startime="" vip_endtime="" boardid="0" isuserpermissionall="0"/><agent browser="Microsoft Internet Explorer" version="6.0" platform="Windows XP" ip="127.0.0.1" lockip="0"/></xml>


一、全程跟踪保存和读取过程

1.用户登录保存:

login.asp中ChkUserLogin函数片断:
Set Dvbbs.UserSession=Dvbbs.RecordsetToxml(rsUser,"userinfo","xml")
Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@cometime").text=Now()
Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@activetime").text=DateAdd("s",-3600,Now())
Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@boardid").text=0
Dvbbs.UserSession.documentElement.selectSingleNode("userinfo").attributes.setNamedItem(Dvbbs.UserSession.createNode(2,"isuserpermissionall","")).text=Dvbbs.FoundUserPermission_All()
If OLDuserhidden <> CLng(userhidden) Then
    Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@userhidden").text=userhidden
    Dvbbs.Execute("update Dv_user set userhidden="&userhidden&" where UserId=" & Dvbbs.UserID)
End If
Dim BS
Set Bs=Dvbbs.GetBrowser()
Dvbbs.UserSession.documentElement.appendChild(Bs.documentElement)
If EnabledSession Then     Session(Dvbbs.CacheName & "UserID")=Dvbbs.UserSession.xml

inc/Dv_ClsMain.asp中RecordsetToxml函数内容:
Public Function RecordsetToxml(Recordset,row,xmlroot)
    Dim i,node,rs,j,DataArray
    If xmlroot="" Then xmlroot="xml"
    If row="" Then row="row"
    Set RecordsetToxml=Server.CreateObject("msxml2.FreeThreadedDOMDocument"& MsxmlVersion)
    RecordsetToxml.appendChild(RecordsetToxml.createElement(xmlroot))
    If Not Recordset.EOF Then
        DataArray=Recordset.GetRows(-1)
        For i=0 To UBound(DataArray,2)
            Set Node=RecordsetToxml.createNode(1,row,"")
            j=0
            For Each rs in Recordset.Fields
                     node.attributes.setNamedItem(RecordsetToxml.createNode(2,LCase(rs.name),"")).text= DataArray(j,i)& ""
                     j=j+1
            Next
            RecordsetToxml.documentElement.appendChild(Node)
        Next
    End If
    DataArray=Null
End Function

说明:ChkUserLogin函数末句"If EnabledSession Then     Session(Dvbbs.CacheName & "UserID")=Dvbbs.UserSession.xml"表示保存于Session中的信息为XML片断而非XML对象。

2.浏览论坛过程中读取信息

inc/Dv_ClsMain.asp中CheckUserLogin函数片断:
If EnabledSession Then
    Set UserSession=Server.CreateObject("msxml2.FreeThreadedDOMDocument"& MsxmlVersion)
    If Not UserSession.loadxml(Session(CacheName & "UserID")&"") Then
        If UserID > 0 Then
            TrueCheckUserLogin
        Else
            Call LetGuestSession()
        End If
    Else
        If UserID >0 or UserSession.documentElement.selectSingleNode("userinfo/@userid").text<>"0"  Then
            Dim NeedToUpdate,toupdate
            toupdate=False
            Name="NeedToUpdate"
            If Not ObjIsEmpty() Then
                NeedToUpdate=","&Value&","
                If InStr(NeedToUpdate,","&MemberName&",")>0 Then
                    Call NeedUpdateList(MemberName,0)
                    toupdate=True
                End If
            End If
            If NeedChecklongin or  toupdate Then TrueCheckUserLogin
        Else

        End If
    End If
Else

说明:注意上边红色的loadxml,它和ChkUserLogin中保存代码片断是相对应的,若ChkUserLogin中
"If EnabledSession Then     Session(Dvbbs.CacheName & "UserID")=Dvbbs.UserSession.xml"改为:
"If EnabledSession Then     Set Session(Dvbbs.CacheName & "UserID")=Dvbbs.UserSession"
则此处也要相应的改为用load方法。至于保存为对象好还是保存成片断好?这边选择的是保存为片断,我暂时没想出原因:)

二、简单的例子

write.asp 代码:
<%
'//创建DOM对象
Dim objDom
Set objDom = Server.CreateObject("MicroSoft.XMLDom")

'//创建根节点
Dim rootNode
Set rootNode = objDom.CreateElement("xml")
objDom.appendChild rootNode

'//创建子节点
Dim nodeChild,nodeChild2
Set nodeChild = objDom.CreateElement("username")
Set nodeChild2 = objDom.CreateElement("userpass")
nodeChild.Text = "wwwmzwucom"
nodeChild2.Text = "666666"
rootNode.appendChild nodeChild
rootNode.appendChild nodeChild2

'//将XMLDom对象赋给Session对象
Set Session("info") = objDom
%>
<a  href="read.asp">read.asp</a>

read.asp代码:
<%
'//创建根节点对象
Dim objRoot
Set objRoot = Session("info").documentElement

Response.Write "UserName:" & objRoot.childNodes.item(0).text & "<br/>"
Response.Write "UserPass:" & objRoot.childNodes.item(1).text
%>


三、后记

以前潜意识中总认为只有加载了实际存在的xml文件后,XMLDom对象的方法和属性才可以用,其实对于用loadxml方法加载进来的XML片断,这些方法和属性也是可用的,像动网,只是平时很少用罢了,甚至不需用load和loadxml加载,对于程序中直接创建的XMLDom对象,这些方法及属性也是可用的,如后边的例子!

评论: 1 | 引用: 0 | 查看次数: 5711
发表评论
登录后再发表评论!