uc_client 客戶端庫。1. 用戶在 App A (Discuz) 登入成功
└─ logging.php → uc_user_login($username, $password) → 返回 $uid
2. App A 呼叫同步登入 API
└─ uc_user_synlogin($uid)
└─ uc_api_post('user', 'synlogin', ['uid' => $uid])
└─ POST 到 UCenter/index.php?m=user&a=synlogin
3. UCenter 處理請求
└─ control/user.php → onsynlogin($uid)
└─ 遍歷應用列表,篩選「啟用同步登入」的應用
└─ 對每個應用產生 JS 代碼:
4. 瀏覽器執行 JS,向各應用發送 GET 請求
└─ App B 的 api/uc.php 接收參數
└─ _authcode($code, 'DECODE', UC_KEY) 解密得到 $uid
└─ _setcookie('auth', $authCode, $expire) 設定跨域 Cookie
5. 用戶訪問 App B 任意頁面
└─ 瀏覽器自動攜帶 Cookie
└─ App B 解密 Cookie 驗證 $uid
└─ 自動登入完成
function uc_api_post($module, $action, $arg = []) {
$url = UC_API . '/index.php?' . http_build_query([
'm' => $module,
'a' => $action,
'time' => time(),
'code' => _authcode(json_encode($arg), 'ENCODE', UC_KEY)
]);
// 使用 fsockopen/cURL 發送 POST,不等待回應 (非阻塞)
return uc_fopen2($url, '', '', '', true, UC_IP, 20);
}
function onsynlogin($uid) {
$apps = $this->getApps('synlogin'); // 取得啟用同步登入的應用
$js = '';
foreach ($apps as $app) {
$code = _authcode("uid=$uid", 'ENCODE', $app['authkey']); // 使用各應用專屬 authkey
$js .= '';
}
return $js;
}
// api/uc.php
require_once './uc_client/client.php';
$code = $_GET['code'] ?? '';
$time = $_GET['time'] ?? 0;
if ($code && (time() - $time) < 3600) { // 時效性檢查
$decoded = _authcode($code, 'DECODE', UC_KEY); // 注意:這裡用 UC_KEY 還是各自 authkey 視實作而定
parse_str($decoded, $params); // uid=123
if (!empty($params['uid'])) {
$uid = intval($params['uid']);
// 關鍵:設定登入 Cookie
// 通常調用應用自身的登入邏輯生成 auth cookie
// 例:Discuz 的 authcode 生成
$auth = authcode("$uid\t" . md5($password), 'ENCODE'); // 簡化
setcookie('auth', $auth, time() + 86400 * 30, '/', '.domain.com'); // 跨子域
}
}
?>
function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ?? UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya . md5($keya . $keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE'
? base64_decode(substr($string, $ckey_length))
: sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
$result = '';
$box = range(0, 255);
$rndkey = [];
for ($i = 0; $i < 256; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for ($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
[$box[$i], $box[$j]] = [$box[$j], $box[$i]];
}
for ($a = $j = $i = 0; $i < strlen($string); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
[$box[$a], $box[$j]] = [$box[$j], $box[$a]];
$result .= chr(ord($string[$i]) ^ $box[($box[$a] + $box[$j]) % 256]);
}
if ($operation == 'DECODE') {
if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) > time()) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
return substr($result, 26);
}
return '';
}
return $keyc . str_replace('=', '', base64_encode($result));
}
| 屬性 | 設定值 | 說明 |
|---|---|---|
| Domain | .example.com | 前綴點號支援所有子域 |
| Path | / | 全站有效 |
| SameSite | Lax / None | 跨站請求需 None + Secure |
| Secure | true | HTTPS 必須 |
| HttpOnly | true | 防 XSS |
UCenter 設計於 2008 年,現代架構建議:
理解 UCenter 同步登入的「中心下發 JS → 瀏覽器執行 → 各應用設定 Cookie」機制,有助於維護舊系統、設計現代 SSO 架構、排查跨域登入失效問題。