自己的网站使用单点登录(cas)进行控制,现在想要增加一个discuz论坛也用单点登录进行控制,为了进行整合dz费尽心思,把dz的登录流程研究了一遍才算大致明白了怎么去做,因为dz有自己的cookie和session机制,不是用的php的原生的,所以这点也着实让我头疼了好久,现在终于搞定了。具体实现的代码如下:
$this->casLogin();
/*
* 单点登录的调用,成功后返回用户名,然后再设置dz的用户登录状态
*/
public function casLogin(){
require_once 'third_party/phpCAS-1.3.0/CAS.php';
define('CAS_SERVER_HOSTNAME', "123.52.117.63");
define('CAS_SERVER_PORT', 8081);
define('CAS_SERVER_APP_NAME', "cas");
phpCAS::setDebug();
phpCAS::client(CAS_VERSION_2_0, CAS_SERVER_HOSTNAME, CAS_SERVER_PORT, CAS_SERVER_APP_NAME);
phpCAS::setNoCasServerValidation();
phpCAS::handleLogoutRequests();
phpCAS::forceAuthentication();
$username = phpCAS::getUser(); //获取cas 服务端用户名
$this->loginLee($username);
}//end func caslee()
/*
* 根据用户名设置dz论坛的登录状态
*/
function loginLee($username){
//define('IN_DISCUZ', true);
require 'source/class/class_core.php';
require_once 'source/function/function_core.php';
$discuz = C::app();
$discuz->init();
$discuz->init_cron = false;
$discuz->init_session = false;
global $_G;
if(!function_exists('uc_user_login')) {
loaducenter();
}
//list($uid, $username, $password, $email) = uc_user_login('leedaning', '199028'); //根据用户名密码进行登录
if($data = uc_get_user($username)) {
list($uid, $username, $email) = $data; //根据用户名获取uid进行登录
} else {
echo '用户不存在';
}
if($uid > 0) {
$member = getuserbyuid($uid, 1); //根据uid获取用户表pre_common_member中的所有字段
//global $_G;
$_G['uid'] = intval($uid);
$_G['username'] = $username;
$_G['adminid'] = $member['adminid'];
$_G['groupid'] = $member['groupid'];
$_G['formhash'] = formhash();
$_G['session']['invisible'] = getuserprofile('invisible');
$_G['member'] = $member;
loadcache('usergroup_'.$_G['groupid']);
C::app()->session->isnew = true;
C::app()->session->updatesession();
dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true); //这里的passwod是pre_common_member表中经过加密后的密码
dsetcookie('loginuser',$username);
dsetcookie('activationauth');
dsetcookie('pmnum');
/* setcookie("usernameLee",$username,time()+intval(24*3600)); */
echo uc_user_synlogin($uid);
echo '登录成功';
} elseif($uid == -1) {
echo '用户不存在,或者被删除';
} elseif($uid == -2) {
echo '密码错';
} else {
echo '未定义';
}
//echo "<script>location.href='forum.php';</script>";
header("location:forum.php");
}//end function loginLee()