discuz 本身提供UCENTER用户中心能够实现单点登录。
可是其它应用要单点登录到discuz还是存在若干问题:
须要2次激活。可能造成server无响应,论坛显示的最新注冊用户无法同步更新,官网没有提供其它语言的api
等
这里提供了段代码。在bbs根文件夹下保存例如以下php代码go.php
<?php /**
* zj53hao 20140418 外部程序单点登录到discuz(同步注冊和登录到discuz) http://blog.csdn.net/zj53hao
*/
define('NOROBOT', FALSE);
define('ADMINSCRIPT', basename(__FILE__));
define('CURSCRIPT', 'admin');
define('HOOKTYPE', 'hookscript');
define('APPTYPEID', 0);
//define('CURMODULE', $mod);
require './source/class/class_core.php';
$discuz = C::app();
$discuz->init();
require libfile('function/member');
require libfile('class/member');
runhooks();
$newusername = trim($_GET['newusername']);
$newpassword = 'www.xxxx.com';//trim($_GET['newpassword']);
$newemail = isset($_GET['newemail'])? strtolower(trim($_GET['newemail'])):$newusername.'@xxx.com';
if(!$newusername || !$newemail) {
showmessage('您眼下未登录xxx网,临时以游客身份仅仅读訪问论坛');
}
// 下面几句防止第3方伪造
$time= (int)($_GET["time"]);
$curdate= time();
$seckey=$time.$newusername.'www.xxx.com';
$seckey= md5($seckey);
if($curdate-$time>1200 || $seckey!=$_GET['code']){
showmessage('submit_invalid');
}
$_G['uid']='';
$userid=C::t('common_member')->fetch_uid_by_username($newusername);
$_SERVER['REQUEST_METHOD'] = 'POST';//注冊须要模拟POST防止2次校验不通过
$_GET['formhash'] = formhash();// 防止 2次校验不通过
$_G['group']['seccode']='';// 防止 2次校验不通过
if(!$userid){// 没有找到相应用户则调用注冊
$_GET['regsubmit']='yes';
$_GET['infloat']='yes';
$_GET['lssubmit']='yes';
$ctl_obj = new register_ctl();
$ctl_obj->setting = $_G['setting'];
$ctl_obj->template = 'member/register';
$_GET[''.$ctl_obj->setting['reginput']['username']]=$newusername;
$_GET[''.$ctl_obj->setting['reginput']['password']]= $newpassword;
$_GET[''.$ctl_obj->setting['reginput']['password2']]= $newpassword;
$_GET[''.$ctl_obj->setting['reginput']['email']] =$newemail;
$ctl_obj->on_register();
}
//uc_user_synlogout();
$_G['groupid'] = $_G['member']['groupid'] = 7;
$_G['uid'] = $_G['member']['uid'] = 0;
$_G['username'] = $_G['member']['username'] = $_G['member']['password'] = '';
// 登陆
$_GET['loginsubmit']='yes';
$_GET['lssubmit']='';
$_GET['username']=$newusername;
$_GET['password']= $newpassword;
$ctl_obj = new logging_ctl();
$ctl_obj->setting = $_G['setting'];
$ctl_obj->template = 'member/login';
$ctl_obj->on_login();
?
>
主要原理是其它WEB应用跳转该URL并带上username。和登录检验串。如
http://xxx。
com/bbs/go.php?newusername=yyyyy&time=1397870932&code=d525745a6c196cb44049c7624bd28ece
在论坛里推断用户是否存在。
不存在则调用注冊模块。存在则登录新用户,假设之前登录过别的用户也会被切换该用户。
当中 使用自己的防止第3方的恶意提交的代码,并通过暂时开关去掉论坛本身的检验代码。
$seckey=$time.$newusername.'www.xxx.com';主要用linux时间戳和用户名。加两方协商好的一个秘钥传再md5加密。匹配后才算验证通过。同样的串仅仅能在1200秒内有效
眼下对discuzX3有效。其它版本号未測试。
浙公网安备 33010602011771号