Posts Tagged ‘’

网站程序轻松集成Discuz

星期四, 04月 3rd, 2008

Discuz是款优秀的BBS论坛程序,很多网站需要和论坛集成,例如显现通行证,但总感觉通过discuz自已提供的的passport现实有点不方便,发了点时间,把各个检测登陆、登陆、退出,做了个概述,希望对各位开发者有所帮助。这里不多说原理了,请看代码里的注释,请注意,此分解是基于Discuz 6.0正式版,如果你使用的其他版本,可能行号不太正确,不过我集成过discuz 5.5,除了行号,其他没什么变化。


/*————————-基础变量————————*/
#用户key,由论坛程序的KEY加上HTTP AGENT组合加密得到的,authcode函数里有引用
#include/common.inc.php 127行
$discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);

#得到并分解cookie,如果没有设置默认值
#include/common.inc.php 128行
list($discuz_pw, $discuz_secques, $discuz_uid) = empty($_DCOOKIE['auth']) ? array(”, ”, 0) : daddslashes(explode(”\t”, authcode($_DCOOKIE['auth'], ‘DECODE’)), 1);
#FORMHASH 全局变量,用来识别用户用的,防止伪造信息
#include/common.inc.php 219行
define(’FORMHASH’, formhash());

#安全问题加密
#logging.php 93行
$secques = quescrypt($questionid, $answer);
/*————————-登陆系统————————*/
#logging.php 104行起
#自己DIY的登陆表单,然后验证,注意,这里默认关闭了安全问题验证,可自行加上

$formUsername = trim($_POST['$username']); // 接受POST传来的$username
$formPassword = trim($_POST['password']); // 接受POST传来的$password
$sql = $db->query(”SELECT uid,password,secques FROM cdb_members WHERE username=’$formUsername’”);
$member = $db->fetch_array($sql);
#验证用户密码是否匹配
if($member['password'] == md5($formPassword))
{
#登陆成功
dsetcookie(’sid’,”,-2423234234); // 注销掉sid
dsetcookie(’auth’, authcode(”$formPasswordt$$member['secques']t$member['uid']“, ‘ENCODE’), ‘1234243′); //加密登陆信息
}
else
{
#登陆失败
}

/*————————-退出系统————————*/
#自己diy退出
<a href=”discuz/logging.php?action=logout&formhash=<?php echo FORMHASH ?>&forward=http://localhost/test/discuz.php”>退出系统</a>
/*————————-检测是否登陆状态————————*/
#检测登陆成功后
#自己diy检测
header(’Content-Type: text/html; charset=utf-8′);
include ‘discuz/include/common.inc.php’; //插入discuz核心文件,这是必须,要不然无法判断

if($discuz_uid)
{
echo ‘你已经登陆<br>’;
echo ‘<a href=”discuz/logging.php?action=logout&formhash=’.formhash().’&forward=http://localhost/test/discuz.php”>退出系统</a><br>’;
}
else
{
echo ‘你还没有登陆<br>’;
echo ‘<a href=”discuz/logging.php?action=login&forward=http://localhost/test/discuz.php”>登陆系统</a><br>’;
}

/*————————-用到的几个函数————————*/
#加密:include/global.func.php
function authcode($string, $operation, $key = ”) {

$key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
$key_length = strlen($key);

$string = $operation == ‘DECODE’ ? base64_decode($string) : substr(md5($string.$key), 0, 8).$string;
$string_length = strlen($string);

$rndkey = $box = array();
$result = ”;

for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($key[$i % $key_length]);
$box[$i] = $i;
}

for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}

if($operation == ‘DECODE’) {
if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) {
return substr($result, 8);
} else {
return ”;
}
} else {
return str_replace(’=', ”, base64_encode($result));
}

}

#清除cookie
function clearcookies() {
global $discuz_uid, $discuz_user, $discuz_pw, $discuz_secques, $adminid, $credits;
dsetcookie(’sid’, ”, -86400 * 365);
dsetcookie(’auth’, ”, -86400 * 365);
dsetcookie(’visitedfid’, ”, -86400 * 365);
dsetcookie(’onlinedetail’, ”, -86400 * 365, 0);

$discuz_uid = $adminid = $credits = 0;
$discuz_user = $discuz_pw = $discuz_secques = ”;
}

#设计cookie
function dsetcookie($var, $value, $life = 0, $prefix = 1) {
global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
setcookie(($prefix ? $cookiepre : ”).$var, $value,
$life ? $timestamp + $life : 0, $cookiepath,
$cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}

#加密安全问题
function quescrypt($questionid, $answer) {
return $questionid > 0 && $answer != ” ? substr(md5($answer.md5($questionid)), 16, 8) : ”;
}