J2EE整合UCenter 首次注册同步登录与自动激活

        J2EE程序整合 UCenter,如果在J2EE系统中在新用户首次注册后是没有同步登录到其它程序的,也就是说用户得在其它程序用密码再登录一次,以后才能同步登录,还有就是用户不是在 Discuz 程序注册的话,在 Discuz 登录时得手动激活,这些都导致用户体验很不好,不过解决方法很简单~
修改各个程序目录下的 ./uc_client/model/user.php 文件,大概在 129 行处的 function add_user 函数里添加代码 (这里要说明的是修改的是./uc_server/model/user.php文件才产生的从Ucenter同步注册到Discuz系统并免激活的效果)

如 Discuz X 的:

$this->db->query("INSERT INTO `dbname`.pre_common_member SET uid='$uid', username='$username', password='$password', email='$email', adminid='0', groupid='10', regdate='".$this->base->time."', credits='0', timeoffset='9999'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_status SET uid='$uid', regip='$regip', lastip='$regip', lastvisit='".$this->base->time."', lastactivity='".$this->base->time."', lastpost='0', lastsendmail='0'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_profile SET uid='$uid'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_field_forum SET uid='$uid'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_field_home SET uid='$uid'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_count SET uid='$uid', extcredits1='0', extcredits2='0', extcredits3='0', extcredits4='0', 
        MySQL 设置相应的权限,假设 Discuz X 和 UCenter 是在不同的数据库且不同数据库用户,Discuz X 数据库和用户是 discuz,UCenter 的数据库和用户是 ucenter,那么需要设置 ucenter 拥有数据库 discuz 的 insert 权限。(如果是同一数据库、同一用户则忽略这些步骤)
phpMyAdmin 的操作步骤大概为:权限 -> 编辑权限 -> 按数据库指定权限 -> 选择数据库 -> 勾选 INSERT -> 执行。

这些代码的原理就是,在某应用注册用户时,同时添加其它应用的数据库字段,因为 UCenter 在首次注册时并没有这一步骤所以才没能同步登录与激活。
修改后完整的 function add_user 函数是这样的:

function add_user($username, $password, $email, $uid = 0, $questionid = '', $answer = '', $regip = '') {
	$regip = empty($regip) ? $this->base->onlineip : $regip;
	$salt = substr(uniqid(rand()), -6);
	$password = md5(md5($password).$salt);
	$sqladd = $uid ? "uid='".intval($uid)."'," : '';
	$sqladd .= $questionid > 0 ? " secques='".$this->quescrypt($questionid, $answer)."'," : " secques='',";
	$this->db->query("INSERT INTO ".UC_DBTABLEPRE."members SET $sqladd username='$username', password='$password', email='$email', regip='$regip', regdate='".$this->base->time."', salt='$salt'");
	$uid = $this->db->insert_id();
	$this->db->query("INSERT INTO ".UC_DBTABLEPRE."memberfields SET uid='$uid'");
	// BEGIN
	$this->db->query("INSERT INTO `dbname`.pre_common_member SET uid='$uid', username='$username', password='$password', email='$email', adminid='0', groupid='10', regdate='".$this->base->time."', credits='0', timeoffset='9999'");
	$this->db->query("INSERT INTO `dbname`.pre_common_member_status SET uid='$uid', regip='$regip', lastip='$regip', lastvisit='".$this->base->time."', lastactivity='".$this->base->time."', lastpost='0', lastsendmail='0'");
	$this->db->query("INSERT INTO `dbname`.pre_common_member_profile SET uid='$uid'");
	$this->db->query("INSERT INTO `dbname`.pre_common_member_field_forum SET uid='$uid'");
	$this->db->query("INSERT INTO `dbname`.pre_common_member_field_home SET uid='$uid'");
	$this->db->query("INSERT INTO `dbname`.pre_common_member_count SET uid='$uid', extcredits1='0', extcredits2='0', extcredits3='0', extcredits4='0', extcredits5='0', extcredits6='0', extcredits7='0', extcredits8='0'");
	// END
	return $uid;
} 
转载自:《解决整合 UCenter 首次注册同步登录与自动激活》

你可能感兴趣的:(J2EE,discuz,ucenter,免激活)