Discuz! Passport 通行证整合(8)

$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;
}
?>
<?php
if(@$_COOKIE['myusername']==''){
?>
<form action="" method="POST">
用户名:<input>测试用户名为admin,请更改为你的用户名<br>
密码:<input type="password">测试密码为123456<br>
<input type="submit" value="测试">
</form>
<?php
}
else echo("登陆成功,<a href='https://www.jb51.net/{$path_bbs}index.php' target=_blank>请到论坛检查登陆</a><br><a href='https://www.jb51.net/?act=logout'>退出登陆</a> ");
?>

第六篇 C#与discuz5.0的整合实例

using System;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// c#与discuz的passport整合事例
/// 请保留以下信息以备出现问题时使用
/// 整合贡献:huozhe3136 QQ:2666556
/// 调试工具:Microsoft Visual Web Developer 2005
/// </summary>
public partial class _Default : System.Web.UI.Page {
    //------------------------需要修改的设置---------------------------------
    string webUrl           = ":1627/webapp/Default.aspx";//这里修改为你的测试主站的地址,不要缺少最后的'https://www.jb51.net/'号
    string dzUrl            = ":8080/discuz/";//这里修改为你的dz地址,不要缺少最后的'https://www.jb51.net/'号
    string dzPassportKey    = "1234567890";//这里修改为你的dz后台通行证设置里的possportkey
    string dzForward        = "";
    string dzAuth           = "";
    string dzVerify         = "";

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a6b433df30aff4795dbf0d07a5abf20e.html