from http://blog.sjzj.com.cn/article.asp?id=492
这几天公司里要我把三个系统整合起来,一个是自己开发的招商引资系统,一个是前任程序员开发的B2B商务程序,还有一个就是动网BBS,所谓整合就是一站式注册,一站式登陆,一站式注销,前两个系统因为是自己公司开发的,都有开发文档在,所以整合起来比较简单,动网总的来说,验证机制还是比较麻烦的!
一开始我上动网官网上搜索的,找到了一个整合文件,不过个人技术太菜,看了一会放弃了,后来又上CSDN,也没有找到很好的解决方案,那么就只有靠自己了!
这几天我一共试了两种办法,下面先说一下第一种办法,
第一种的思路是这样的,在网站登陆的时候,写Cookies,访问论坛的时候读这个Cookies,如果存在的话,就查询动网的用户表,然后写动网自己的Cookies和Session,实现的步骤是这样的:
1.第一步当然是同步用户表呀。
2.在网站登陆验证页面写下如下语名
'-----------------------------------------------------------------
'同步登陆 修改于(2006-06-28 15:54:00)
'-----------------------------------------------------------------
Response.Cookies("Zjing").Domain = " Zjing. net " 这里域名设置(指定该Cookies只定zjing.com有效)
Response.Cookies("Zjing ").Expires = date()+365
Response.Cookies("Zjing ")("ChinaUserName") = 用户名
Response.Cookies("Zjing ")("ChinaPassword") =Md5加密的密码
'-----------------------------------------------------------------
'同步登陆 修改于(2006-06-28 15:54:00)
'-----------------------------------------------------------------
3.用Dw整站替换的功能将动网所有的
引用内容
Response.Cookies(Dvbbs.Forum_sn).path = Dvbbs.cookiepath
替换为
Response.Cookies("Zjing").Domain = " Zjing. net "
4.新建一个WriteCookies.asp的文件放在动网根目录。将下面这段代码复制进去
<%
'-----------------------------------------------------------------
'同步登陆 修改于(2006-06-28 15:32:00)
'-----------------------------------------------------------------
session("flag")=empty
Response.Cookies("StatUserID")=Request.Cookies("StatUserID")
Dim ChinaUserName,ChinaPassword,TruePassWordChina
ChinaUserName = Trim(Request.Cookies("Zjing ")("ChinaUserName"))
ChinaPassword = Trim(Request.Cookies("Zjing ")("ChinaPassword"))
If ChinaUserName<> "" and ChinaPassword <> "" then
If ChkChina228Login(ChinaUserName,ChinaPassword,1,ChinaCreatepass(),1) then
End If
End if
Function ChkChina228Login(ChinaUserName,ChinaPassword,usercookies,TruePassWordChina,userhidden)
Dim rsUser,article,userclass,titlepic
Dim lastip,UserLastLogin
Dim GroupID,ClassSql,FoundGrade
Dim regname,iMyUserInfo
Dim sql,sqlstr,OLDuserhidden
Sql="Select UserID,UserName,UserPassword,UserEmail,UserPost,UserTopic,UserSex,UserFace,UserWidth,UserHeight,JoinDate,LastLogin,lastlogin as cometime , LastLogin as activetime,UserLogins,Lockuser,Userclass,UserGroupID,UserGroup,userWealth,userEP,userCP,UserPower,UserBirthday,UserLastIP,UserDel,UserIsBest,UserHidden,UserMsg,IsChallenge,UserMobile,TitlePic,UserTitle,TruePassWord,UserToday,UserMoney,UserTicket,FollowMsgID,Vip_StarTime,Vip_EndTime,userid as boardid"
Sql=Sql & " From [Dv_User] Where UserName='"&ChinaUserName&"'"
set rsUser=Dvbbs.Execute(sql)
If rsUser.eof and rsUser.bof Then
ChkChina228Login=False
Exit Function
Else
If rsUser("Lockuser") =1 or rsUser("UserGroupID") =5 Then
ChkChina228Login=False
Exit Function
Else
If Trim(ChinaPassword)=Trim(rsUser("UserPassword")) Then
ChkChina228Login=True
Dvbbs.UserID=RsUser("UserID")
RegName = RsUser("UserName")
Article= RsUser("UserPost")
UserLastLogin = RsUser("cometime")
UserClass = RsUser("Userclass")
GroupID = RsUser("userGroupID")
OLDuserhidden=RsUser("UserHidden")
TitlePic = RsUser("UserTitle")
If Article < 0 Then Article=0
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
Else
ChkChina228Login=False
Exit Function
End If
End If
End If
If ChkChina228Login Then
Set rsUser=Dvbbs.Execute("Select MinArticle,IsSetting,ParentGID,UserTitle,GroupPic From Dv_UserGroups Where UserGroupID="&GroupID)
If Not (rsUser.Eof And rsUser.Bof) Then
If rsUser(2)=1 or rsUser(2)=2 or rsUser(2)=4 or rsUser(2)=5 Then
'用户等级不按照文章升级,用户为系统或特殊或多属性组
UserClass=rsUser(3)
TitlePic=rsUser(4)
FoundGrade=True
End If
End If
If Not FoundGrade Then
'如果不属于系统或特殊或多属性组,则将该用户属于注册用户组且按照其文章数自动更新其用户组(等级)
Set rsUser=Dvbbs.Execute("Select Top 1 usertitle,GroupPic,UserGroupID From Dv_UserGroups Where ParentGID=3 And Minarticle<="&Article&" order By MinArticle Desc,UserGroupID")
If Not (rsUser.Eof And rsUser.Bof) Then
UserClass=rsUser(0)
TitlePic=rsUser(1)
GroupID=rsUser(2)
FoundGrade=True
End If
End If
Set rsUser=nothing
If Not FoundGrade Then
ChkChina228Login=False
Exit Function
end if
sql="update [Dv_User] set LastLogin="&SqlNowString&",UserLogins=UserLogins+1,UserLastIP='"&lastip&"',userclass='"&userclass&"',titlepic='"&titlepic&"',UserGroupID="&GroupID&",TruePassWord='"&TruePassWordChina&"' where userid="&dvbbs.UserID
Dvbbs.Execute(sql)
Dim StatUserID,UserSessionID
StatUserID = Dvbbs.checkStr(Trim(Request.Cookies(Dvbbs.Forum_sn)("StatUserID")))
If IsNumeric(StatUserID) = 0 or StatUserID = "" Then
StatUserID = Replace(Dvbbs.UserTrueIP,".","")
UserSessionID = Replace(Startime,".","")
If IsNumeric(StatUserID) = 0 or StatUserID = "" Then StatUserID = 0
StatUserID = Ccur(StatUserID) + Ccur(UserSessionID)
End If
StatUserID = Ccur(StatUserID)
Dvbbs.Execute("delete from dv_online where id="&StatUserID&"")
If trim(ChinaUserName)<>trim(Dvbbs.membername) Then
Response.Cookies(Dvbbs.Forum_sn)("username")=""
Response.Cookies(Dvbbs.Forum_sn)("password")=""
Response.Cookies(Dvbbs.Forum_sn)("userclass")=""
Response.Cookies(Dvbbs.Forum_sn)("userid")=""
Response.Cookies(Dvbbs.Forum_sn)("userhidden")=""
Response.Cookies(Dvbbs.Forum_sn)("usercookies")=""
Dvbbs.Execute("delete from dv_online where username='"&Dvbbs.membername&"'")
End If
If isnull(usercookies) or usercookies="" Then usercookies="0"
select case usercookies
case "0"
Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
case 1
Response.Cookies(Dvbbs.Forum_sn).Expires=Date+1
Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
case 2
Response.Cookies(Dvbbs.Forum_sn).Expires=Date+31
Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
case 3
Response.Cookies(Dvbbs.Forum_sn).Expires=Date+365
Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
end select
Response.Cookies(Dvbbs.Forum_sn).Domain = "china228.com"
Response.Cookies(Dvbbs.Forum_sn)("StatUserID") = StatUserID
Response.Cookies(Dvbbs.Forum_sn)("username") = Regname
Response.Cookies(Dvbbs.Forum_sn)("password") = TruePassWordChina
Response.Cookies(Dvbbs.Forum_sn)("userclass") = userclass
Response.Cookies(Dvbbs.Forum_sn)("userid") = Dvbbs.UserID
Response.Cookies(Dvbbs.Forum_sn)("userhidden") = 2
rem 清除图片上传数的限制
Response.Cookies("upNum")=0
Dvbbs.Membername=Dvbbs.Checkstr(regname)
Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@truepassword").text= TruePassWordChina
Dvbbs.Memberclass=Dvbbs.Checkstr(userclass)
Dvbbs.UserGroupID=GroupID
End If
End Function
Function ChinaCreatepass()'系统分配随机密码
Dim Ran,i,LengthNum
LengthNum=16
ChinaCreatepass=""
For i=1 To LengthNum
Randomize
Ran = CInt(Rnd * 2)
Randomize
If Ran = 0 Then
Ran = CInt(Rnd * 25) + 97
ChinaCreatepass = ChinaCreatepass& UCase(Chr(Ran))
ElseIf Ran = 1 Then
Ran = CInt(Rnd * 9)
ChinaCreatepass = ChinaCreatepass & Ran
ElseIf Ran = 2 Then
Ran = CInt(Rnd * 25) + 97
ChinaCreatepass =ChinaCreatepass& Chr(Ran)
End If
Next
End Function
'-----------------------------------------------------------------
'同步登陆 修改于(2006-06-28 15:32:00)
'-----------------------------------------------------------------
%>
上面的代段我就不来分析了,我和大家教一下动网的验证机制吧,动网是Cookies配合Session进行验证的,
Cookies:
Response.Cookies(Dvbbs.Forum_sn).Domain = "Zjing.net" Cookies所在域名
Response.Cookies(Dvbbs.Forum_sn)("StatUserID") = StatUserID 用户在线ID
Response.Cookies(Dvbbs.Forum_sn)("username") = Regname 用户名
Response.Cookies(Dvbbs.Forum_sn)("password") = TruePassWordChina 随机生成的密码,在程序中会和数据库中进行判断
Response.Cookies(Dvbbs.Forum_sn)("userclass") = userclass 用户等级
Response.Cookies(Dvbbs.Forum_sn)("userid") = Dvbbs.UserID 用户名
Response.Cookies(Dvbbs.Forum_sn)("userhidden") = 2 是否在线
Session:
Session(Dvbbs.CacheName & "UserID")
他储存的是一个Xml文档
<xml><userinfo userid="14424" username="用户名" userpassword="密码" useremail="Email" userpost="发贴数" usertopic="主题" usersex="性别" userface="头像" userwidth="头像宽" userheight="头像高" joindate="注册时间" lastlogin="最后登陆时间" cometime="2006-6-28 22:48:39" activetime="活动时间" userlogins="登陆次数" lockuser="0" userclass="版主" usergroupid="3" usergroup="" userwealth="1504" userep="766" usercp="400" userpower="0" userbirthday="" userlastip="220.184.65.190" userdel="0" userisbest="0" userhidden="1" usermsg="" ischallenge="0" usermobile="" titlepic="level18.gif" usertitle="" truepassword="BfVIy4k886ZuD956" usertoday="33|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 2003" ip="220.184.65.190" lockip="0"/></xml>
5.打开inc/const.asp,在第二行加<!--#include file="../WriteCookies.asp"-->
Const.asp的完整代码为
<!--#Include File="Dv_ClsMain.asp"-->
<!--#include file="../WriteCookies.asp"-->
<%
Set MyBoardOnline=new Cls_UserOnlne
Dvbbs.GetForum_Setting
Dvbbs.CheckUserLogin
%>
到此整合基本上已经可以了,在这里需要特别注意的就是Cookies的域设置,因为我的站比较特别,不是同一个域名,而是有很多的子域名组成的,比如主站www,论坛bbs,所以这里域一定要设置正确,不然三个系统就不能正确读取Cookies了