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

多程序整合 UCenter,在新用户首次注册后是没有同步登录到其它程序的,也就是说用户得在其它程序用密码再登录一次,以后才能同步登录,还有就是用户不是在 Discuz 程序注册的话,在 Discuz 登录时得手动激活,这些都导致用户体验很不好,不过解决方法很简单~
修改各个程序目录下的 ./uc_client/model/user.php 文件,大概在 129 行处的 function add_user 函数里添加代码
如 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', extcredits5='0', extcredits6='0', extcredits7='0', extcredits8='0'");

Discuz 的参数比较多,`dbname` 是数据库名,.pre_ 是表前缀,按自己的情况修改,注意需要在 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;
}

其它的程序就比较简单了,打开程序的数据库的用户表,对比一下就知道了,下面提供几个参考:

// ecmall
$this->db->query("INSERT INTO `ecmall`.ecm_member SET user_id='$uid', user_name='$username', email='$email', password='$password', gender='0', reg_time='".$this->base->time."', last_login='".$this->base->time."', last_ip='$regip', logins='1', ugrade='0', outer_id='0'");

// ecshop
$this->db->query("INSERT INTO `ecshop`.ecs_users SET user_id='$uid', email='$email', user_name='$username', password='$password', reg_time='".$this->base->time."', last_login='".$this->base->time."', last_ip='$regip'");

// thinksns
$this->db->query("INSERT INTO `thinksns`.ts_ucenter_user_link SET uid='$uid', uc_uid='$uid', uc_username='$username'");
$this->db->query("INSERT INTO `thinksns`.ts_user SET uid='$uid', email='$email', password='$password', uname='$username', is_active='1', is_init='1'");
$this->db->query("INSERT INTO `thinksns`.ts_user_online SET uid='$uid', ctime='".$this->base->time."'");

(需要说明的是部分程序的登录机制不同使得此方法的首次注册同步登录无法实现)

邮件订阅,随时获取更新信息

发表评论

电子邮件地址不会被公开。

This site uses Akismet to reduce spam. Learn how your comment data is processed.