discuz论坛与其它网站登录注册整合
本文以discuz 7.0.0 php版本的论坛与 .net 2.0的网站注册登录整合为类.
没有采用uc_center或第三方插件.以另类的方式实现.
此方法实现的好处是不用过多了解论坛本身的实现细节,借助论坛自己的登录,注册页面来完成,我们只是获到参数post到这个页面来实现注册及登录过程,修改密码也可以用这个方式,但考虑到修改相对简单,就用直接修改数据库的方式实现.
一、论坛修改的地方
1. 论坛入口修改
a. templates/default/header.htm 修改注册和登录入口到网站
b. forumdata/ templates/1_discuz.tpl.php 修改注册和登录的入口
2. register.php改成如下:
<?php
/*
[Discuz!] (C)2001-2009 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: redirect.php 16688 2008-11-14 06:41:07Z cnteacher $
*/
define('CURSCRIPT', 'viewthread');
require_once './include/common.inc.php';
if($goto == 'findpost') {
$pid = intval($pid);
$ptid = intval($ptid);
if($post = $db->fetch_first("SELECT p.tid, p.dateline, t.special FROM {$tablepre}
posts p LEFT JOIN {$tablepre}threads t USING(tid) WHERE p.pid='$pid'")) {
$sqladd = $post['special'] ? "AND first=0" : '';
$page = ceil($db->result_first("SELECT count(*) FROM {$tablepre}posts WHERE
tid='$post[tid]' AND dateline<='$post[dateline]' $sqladd") / $ppp);
if(!empty($special) && $special == 'trade') {
dheader("Location: viewthread.php?do=tradeinfo&tid=$post[tid]
&pid=$pid");
} else {
dheader("Location: viewthread.php?tid=$post[tid]&page=$page".
(isset($_GET['modthreadkey']) && ($modthreadkey=modthreadkey($post['tid'])) ?
"&modthreadkey=$modthreadkey": '')."#pid$pid");
}
} else {
$ptid = !empty($ptid) ? intval($ptid) : 0;
showmessage('post_check', NULL, 'HALTED');
}
}
$tid = $forum['closed'] < 2 ? $tid : $forum['closed'];
if(empty($tid)) {
showmessage('thread_nonexistence');
}
if(isset($fid) && empty($forum)) {
showmessage('forum_nonexistence', NULL, 'HALTED');
}
@include DISCUZ_ROOT.'./forumdata/cache/cache_viewthread.php';
if($goto == 'lastpost') {
if($tid) {
$query = $db->query("SELECT tid, replies, special FROM {$tablepre}threads
WHERE tid='$tid' AND displayorder>='0'");
} else {
$query = $db->query("SELECT tid, replies, special FROM {$tablepre}threads
WHERE fid='$fid' AND displayorder>='0' ORDER BY lastpost DESC LIMIT 1");
}
if(!$thread = $db->fetch_array($query)) {
showmessage('thread_nonexistence');
}
$page = ceil(($thread['special'] ? $thread['replies'] : $thread['replies'] + 1) /
$ppp);
$tid = $thread['tid'];
require_once DISCUZ_ROOT.'./viewthread.php';
exit();
} elseif($goto == 'newpost') {
$page = max(ceil($db->result_first("SELECT COUNT(*) FROM {$tablepre}posts WHERE
tid='$tid' AND dateline<='$lastvisit'") / $ppp), 1);
require_once DISCUZ_ROOT.'./viewthread.php';
exit();
} elseif($goto == 'nextnewset') {
if($fid && $tid) {
$this_lastpost = $db->result_first("SELECT lastpost FROM {$tablepre}threads
WHERE tid='$tid' AND displayorder>='0'");
if($next = $db->fetch_first("SELECT tid FROM {$tablepre}threads WHERE
fid='$fid' AND displayorder>='0' AND lastpost>'$this_lastpost' ORDER BY lastpost ASC LIMIT
1")) {
$tid = $next['tid'];
require_once DISCUZ_ROOT.'./viewthread.php';
exit();
} else {
showmessage('redirect_nextnewset_nonexistence');
}
} else {
showmessage('undefined_action', NULL, 'HALTED');
}
} elseif($goto == 'nextoldset') {
if($fid && $tid) {
$this_lastpost = $db->result_first("SELECT lastpost FROM {$tablepre}threads
WHERE tid='$tid' AND displayorder>='0'");
if($last = $db->fetch_first("SELECT tid FROM {$tablepre}threads WHERE
fid='$fid' AND displayorder>='0' AND lastpost<'$this_lastpost' ORDER BY lastpost DESC LIMIT
1")) {
$tid = $last['tid'];
require_once DISCUZ_ROOT.'./viewthread.php';
exit();
} else {
showmessage('redirect_nextoldset_nonexistence');
}
} else {
showmessage('undefined_action', NULL, 'HALTED');
}
} else {
showmessage('undefined_action', NULL, 'HALTED');
}
?>
修改register.php页面,主要是去掉上面的if判断请求来路
二、网站本身修改的地方
1.登录
a.直接从本站登录:把用户名和密码加密后存入session, 点击导航上 论坛链接 转到bbslogin.aspx
页面
b.如果是从论坛网站登录。直接请求BBSlogin.aspx登录论坛并打开登录页面。Window.open();
{
存入session
string name = Common.DEncrypt.DESEncrypt.Encrypt(username.Text.Trim().ToString(),
"efdsfdedasf48545111111"); //用户名加密
string pass = Common.DEncrypt.DESEncrypt.Encrypt
(txtPassword.Text.Trim().ToString(), "efdsfdedasf48545111111");//密码加密
Session["name"] = name;
Session["pass"] = pass;
}
{
//网站登录成功之后,进行论坛登录 用户是从论坛过来的
if (ViewState["url"].ToString().IndexOf("xxx.com") != -1)
{
string u_name = Common.DEncrypt.DESEncrypt.Encrypt
(username.Text.Trim(), "efdsfdedasf48545111111"); //用户名加密
string u_pass = Common.DEncrypt.DESEncrypt.Encrypt
(txtPassword.Text.Trim(), "efdsfdedasf48545111111");//密码加密
Page.ClientScript.RegisterStartupScript(GetType(), "bbskey",
"<script>window.open('/User/BBSLogoin.aspx?n=" + u_name + "&p=" + u_pass +
"');window.location.href='/User/index.aspx'</script>", false);
}
}
{ 登录
protected void Page_Load(object sender, EventArgs e)
{
ShowUserInfo();
}
public void ShowUserInfo()
{
string u_name = Session["name"].ToString();
string u_pass = Session["pass"].ToString();
Response.Redirect(SitePath + "User/BBSLogoin.aspx?n=" + u_name + "&p=" + u_pass +
"");
Response.Redirect(SitePath + "login.aspx");
}
}
bbslogin.aspx前台页面内容
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BBSLogoin.aspx.cs"
Inherits="User_BBSLogoin" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>正在登录论坛</title>
<script>
function loginbbs()
{
var form = document.getElementById("loginform");
form.submit();
}
if(document.readyState == "complete" )
{
loginbbs();
}
</script>
</head>
<body onload="loginbbs()">
<div style="display:none;">
<form action="http://论坛url/logging.php?
action=login&loginsubmit=yes&floatlogin=yes"
id="loginform" name="login" method="post">
<h3 id="returnmessage">
用户登录<%=Username %><%=Pwd %></h3>
<input type="hidden" value="21e0ca66" name="formhash">
<input type="hidden" value="http://论坛url/index.php" name="referer">
<div class="loginform nolabelform">
<div class="selectinput">
<select selecti="0" name="loginfield" style="float: left; width: 50px; display:
none;"
id="loginfield">
<option value="username"></option>
</select><a tabindex="1" onclick="loadselect_viewmenu(this, 'loginfield', 0,
'floatlayout_login');doane(event)"
onkeyup="loadselect_key(this, event, 'loginfield', 'floatlayout_login')"
onmouseout="this.blur()"
onmouseover="this.focus()" onblur="loadselect_keyinit(event, 2)"
onfocus="loadselect_keyinit(event, 1)"
id="loginfield_selectinput" class="loadselect" hidefocus="true"
href="javascript:;">用户名</a><ul
style="display: none;" id="loginfield_selectmenu" class="newselect"
onblur="loadselect_keyinit(event, 2)"
onfocus="loadselect_keyinit(event, 1)">
<li onclick="loadselect_liset('loginfield', 0,
'loginfield','username',this.innerHTML, 0)"
k_value="username" k_id="loginfield" class="current">用户名</li><li
onclick="loadselect_liset('loginfield', 0, 'loginfield','uid',this.innerHTML, 1)"
k_value="uid" k_id="loginfield">UID</li></ul>
<input type="text" tabindex="1" class="txt" size="36" autocomplete="off"
name="username" id="username" value="<%=Username %>" />
</div>
<p class="selectinput loginpsw">
<label for="password3">
密 码 :</label>
<input type="password" tabindex="1" class="txt" size="36" name="password"
id="password3" value="<%=Pwd %>">
</p>
<div class="selecttype">
<select style="width: 175px; display: none;" selecti="0" id="questionid"
name="questionid"
change="if($('questionid').value > 0) {$('answer').style.display='';}
else {$('answer').style.display='none';}">
<option value="0"></option>
</select><a tabindex="1" onclick="loadselect_viewmenu(this, 'questionid', 0,
'floatlayout_login');doane(event)"
onkeyup="loadselect_key(this, event, 'questionid', 'floatlayout_login')"
onmouseout="this.blur()"
onmouseover="this.focus()" onblur="loadselect_keyinit(event, 2)"
onfocus="loadselect_keyinit(event, 1)"
id="questionid_selectinput" class="loadselect" hidefocus="true"
href="javascript:;">安全提问</a><ul
style="display: none;" id="questionid_selectmenu" class="newselect"
onblur="loadselect_keyinit(event, 2)"
onfocus="loadselect_keyinit(event, 1)">
<li onclick="loadselect_liset('questionid', 0,
'questionid','0',this.innerHTML, 0)"
k_value="0" k_id="questionid" class="current">安全提问</li><li
onclick="loadselect_liset('questionid', 0, 'questionid','1',this.innerHTML, 1)"
k_value="1" k_id="questionid">母亲的名字</li><li
onclick="loadselect_liset('questionid', 0, 'questionid','2',this.innerHTML, 2)"
k_value="2" k_id="questionid">爷爷的名字</li><li
onclick="loadselect_liset('questionid', 0, 'questionid','3',this.innerHTML, 3)"
k_value="3" k_id="questionid">父亲出生的城市</li><li
onclick="loadselect_liset('questionid', 0, 'questionid','4',this.innerHTML, 4)"
k_value="4" k_id="questionid">您其中一位老师的名字
</li><li onclick="loadselect_liset('questionid', 0, 'questionid','5',this.innerHTML, 5)"
k_value="5" k_id="questionid">您个人计算机的型
号</li><li onclick="loadselect_liset('questionid', 0, 'questionid','6',this.innerHTML, 6)"
k_value="6" k_id="questionid">您最喜欢的餐
馆名称</li><li onclick="loadselect_liset('questionid', 0, 'questionid','7',this.innerHTML,
7)"
k_value="7" k_id="questionid">驾驶执照
的最后四位数字</li></ul>
</div>
<p>
<input type="text" tabindex="1" class="txt" size="36" autocomplete="off"
style="display: none;"
id="answer" name="answer"></p>
</div>
<p class="fsubmit">
<button tabindex="1" value="true" name="loginsubmit" type="submit" class="submit">
登录</button>
<input type="checkbox" value="2592000" tabindex="1" id="cookietime"
name="cookietime"
class="checkbox">
<label for="cookietime">
记住我的登录状态</label>
</p>
</form>
</div>
<div style="text-align:center;">
<img src="/images/jdt.gif" />
</div>
</body>
</html>
BBSlogin.aspx后台内容
protected string Username;
protected string Pwd;
protected void Page_Load(object sender, EventArgs e)
{
if (Request["n"] != null)
{
Username = Common.DEncrypt.DESEncrypt.Decrypt(Request["n"].ToString(),
"efdsfdedasf48545111111");
}
if (Request["p"] != null)
{
Pwd = Common.DEncrypt.DESEncrypt.Decrypt(Request["p"].ToString(),
"efdsfdedasf48545dfesfe");
}
Page.RegisterStartupScript("key", "<script>loginbbs();</script>");
}
2.注册的修改 (用户名密码邮箱) 传入BBSRegister.aspx 进行注册
BBSRegister.aspx前台内容
{ 注册
string bbs_uName = Common.DEncrypt.DESEncrypt.Encrypt
(this.txtLogName.Value,"edfck8745debc778888"); //论坛用户名(加密)
string bbs_uPass = Common.DEncrypt.DESEncrypt.Encrypt
(this.password.Value, "edfck8745debc778888"); //论坛密码
string bbs_uEmail = this.email.Value.Trim(); //论坛邮箱
// Page.ClientScript.RegisterStartupScript(GetType(), "mykey",
"<script>alert('注册成功!请通过邮件激活后登录至用户中心完善您的资料');window.open('" +
URLReturn + "');window.open('user/BBSRegister.aspx?
b_N="+bbs_uName+"&b_P="+bbs_uPass+"&b_E="+bbs_uEmail+"')</script>");
Page.ClientScript.RegisterStartupScript(GetType(), "mykey", "<script>alert
('注册成功!请通过邮件激活后登录至用户中心完善您的资料');window.open
('user/BBSRegister.aspx?b_N=" + bbs_uName + "&b_P=" + bbs_uPass + "&b_E=" + bbs_uEmail +
"')</script>");
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BBSRegister.aspx.cs"
Inherits="User_BBSRegister" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>正在同步登录论坛</title>
<script type="text/javascript">
function regbbs()
{
var form = document.getElementById("registerform");
form.submit();
}
if(document.readyState == "complete" )
{
debugger;
regbbs();
}
</script>
</head>
<body onload="regbbs()">
<div style="display:none;">
<form action="http://论坛url/register.php?regsubmit=yes"
id="registerform"
name="register" method="post">
<h3 id="returnmessage4">
注册</h3>
<input type="hidden" value="bae784cb" name="formhash" />
<input type="hidden" value="http://论坛url/index.php" name="referer" />
<input type="hidden" value="register" name="handlekey" />
<input type="hidden" value="true" name="activationauth" />
<div class="loginform regform">
<div>
<span id="activation_hidden">
<label>
<em>用户名:</em><input type="text" tabindex="1" value="<%=Username
%>" maxlength="15"
size="25" autocomplete="off" name="username" id="username" />
*</label>
<label>
<em>密码:</em><input type="password" tabindex="1" id="password"
size="25" name="password"
value="<%=Pwd %>" />
*</label>
<label>
<em>确认密码:</em><input type="password" tabindex="1" value="<%=Pwd
%>" id="password2"
size="25" name="password2" />
*</label>
<label>
<em>Email:</em><input type="text" tabindex="1" id="email" size="25"
name="email"
value="<%=Email %>" />
*</label>
</span>
</div>
</div>
<p>
<span id="reginfo_a_btn"><em> </em>
<button tabindex="1" value="true" name="regsubmit" type="submit"
id="registerformsubmit">
提交</button>
</span>
</p>
</form>
</div>
<div style="text-align: center;">
<img src="/images/jdt.gif" />
</div>
</body>
</html>
BBSRedister.aspx.cs内容
protected string Username;
protected string Pwd;
protected string Email;
protected void Page_Load(object sender, EventArgs e)
{
if (Request["b_N"] != null)
{
Username = Common.DEncrypt.DESEncrypt.Decrypt(Request["b_N"],
"edfck8745debc778888"); //解密用户名
}
if (Request["b_P"] != null)
{
Pwd = Common.DEncrypt.DESEncrypt.Decrypt(Request["b_P"],
"edfck8745debc778888");//解密密码
}
if (Request["b_E"] != null)
{
Email = Request["b_E"];
}
}
3. 用户密码的修改 (直接操作数据库;注意discuz密码加密方式先用新密码a进行一次MD5加密32
位,然后用a+(“discuz_uc_members表中的salt字段的值”))在进行一次MD5加密
public void BBSPwdUpdate(string name, string pwd)
{
string constr =
"host=121.9.1.1;database=discuz;UserName=dbusername;password=123456";
string res = GetRegistrerCodeByName(UserName.ToString());
string pwd1 = Common.StrHelper.Md5(pwd, 32).ToString();
string pwd2 = Common.StrHelper.Md5((pwd1 + res), 32).ToString();
string sql = "update cdb_members set password ='" + pwd2 + "' where username = '" +
name + "'";
string sql1 = "update cdb_uc_members set password ='" + pwd2 + "' where username =
'" + name + "'";
using (MySqlConnection con = new MySqlConnection(constr))
{
try
{
con.Open();
MySqlCommand cmd = new MySqlCommand(sql, con);
MySqlCommand cmd1 = new MySqlCommand(sql1, con);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
}
catch (MySqlException ex)
{
throw new Exception(ex.Message);
}
}
}
public string GetRegistrerCodeByName(string name)
{
string constr =
"host=121.9.1.1;database=discuz;UserName=dbusername;password=123456";
string sql = "select salt from cdb_uc_members where username='" + name + "'";
string res = "";
using (MySqlConnection con = new MySqlConnection(constr))
{
try
{
con.Open();
MySqlCommand com = new MySqlCommand(sql, con);
res = com.ExecuteScalar().ToString();
}
catch (MySqlException ex)
{
throw new Exception(ex.Message);
}
}
return res;
}
三、mysql远程连接失败解决方法
1。改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那
台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从“localhost”
改称“%”
mysql -u root -pvmwaremysql>use mysql;
mysql>update user set host = ‘%’ where user = ‘root’;
mysql>select host, user from user;
2. 授权法。例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@'%’ IDENTIFIED BY ‘mypassword’ WI
TH GRANT OPTION;
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@'192.168.1.3′ IDENTIFIED BY
‘mypassword’ WITH GRANT OPTION;
我用的第一个方法,刚开始发现不行,在网上查了一下,少执行一个语句 mysql>FLUSH PRIVILEGES
使修改生效.就可以了
个人中心的 密码及安全问题链接去掉
把templates 下的 personal_navbar.htm 下的
<li><a href="memcp.php?action=profile&typeid=1">{lang
memcp_profile_security}</a></li>
去掉,
如果不更新缓存把
1_memcp_profile.tpl.php 中的该链接也去掉
其它有登录,注册链接要改掉的话方法类似,找到对应的模板改掉即可,有时需要更新下缓存.