discuz新的單點論壇(不依賴UCenter)
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‘])):[email protected]; 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有效。其它版本號未測試。
discuz新的單點論壇(不依賴UCenter)