//discuz同服务器下无passport登陆整合方法
//贡献:huozhe3136 QQ:2666556
//假设discuz安装在网站根目录下的bbs/中
//用法事例
error_reporting(E_ALL);
$path_bbs="bbs/";//请修改为你的论坛的相对路径
$check_username="admin";//测试时这个用户名必须在论坛里也存在
$check_password="123456";
define('IN_DISCUZ', TRUE);//为了包含discuz下的文件这个必须定义
require_once "$path_bbs/config.inc.php";//获得论坛数据库的配置
require_once "$path_bbs/include/db_mysql.class.php";
if(@$_GET[act]=="logout"){
foreach ($_COOKIE as $key => $value) {
setcookie($key,'',time() - 3600,$cookiepath,$cookiedomain,0);
header("Location:".$_SERVER['PHP_SELF']);
}
}
if(isset($_POST["submit"])){//检查是否点击了提交按钮
$username=trim($_POST['username']);
$pwd=trim($_POST['password']);
//主站验证登陆,为简单起见,这里只作简单的示范
if($username==$check_username && $pwd==$check_password){//测试时这个用户名必须在论坛里也存在
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);//$dbhost,$dbuser,$dbpw,$dbname,$pconnect是在bbs/config.inc.php 里定义的变量
$sql="select * from {$tablepre}members where username='$username'";
$rs = $db->query($sql);
if($dzmember = $db->fetch_array($rs)){
setcookie('myusername',$username,time()+3600,$cookiepath,$cookiedomain,0);//设置主站自身的cookie
setcookie("{$tablepre}sid",'',time() - 3600,$cookiepath,$cookiedomain,0);//让discuz的sid过期是为了让discuz重新分配一个新的sid
setcookie("{$tablepre}auth", authcode("$dzmember[password]\t$dzmember[secques]\t$dzmember[uid]", 'ENCODE'),time()+3600,$cookiepath,$cookiedomain,0);//$cookiepath和$ cookiedomainbbs/config.inc.php里定义的变量
header("Location:".$_SERVER['REQUEST_URI']);
}
}
}
//下面这个函数是discuz的用户cookie编码函数,已修改,原型在$path_bbs/include/global.func.php
function authcode($string, $operation) {
global $_SERVER, $_DCACHE,$path_bbs;
require_once "$path_bbs/forumdata/cache/cache_settings.php";//需要获取论坛的authkey
$discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);
$coded = '';
$keylength = strlen($discuz_auth_key);
$string = $operation == 'DECODE' ? base64_decode($string) : $string;
for($i = 0; $i < strlen($string); $i += $keylength) {
$coded .= substr($string, $i, $keylength) ^ $discuz_auth_key;
}
$coded = $operation == 'ENCODE' ? str_replace('=', '', base64_encode($coded)) : $coded;
return $coded;
}
?>
if(@$_COOKIE['myusername']==''){
?>
用户名:测试用户名为admin,请更改为你的用户名
密码:测试密码为123456
}
else echo("登陆成功,请到论坛检查登陆退出登陆 ");
?>