使用单点登录(sso cas )整合dz论坛

自己的网站使用单点登录(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()







评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值