UCenter 提供兩種接口連接方式,根據部署拓撲選擇:
| 模式 | 適用場景 | 性能 | 配置複雜度 |
|---|---|---|---|
| MySQL 直連 | 同機/同局域網、擁有遠程 DB 權限 | 極高(微秒級) | 中(需 DB 連接信息) |
| 遠程 API | 跨公網、無 DB 權限、異構語言、容器化部署 | 高(毫秒級,含 HTTP 開銷) | 低(僅需 URL/Key/IP) |
// 連接方式:'mysql' 或 空字符串(遠程)
define('UC_CONNECT', 'mysql');
// MySQL 模式專用
define('UC_DBHOST', 'localhost');
define('UC_DBUSER', 'uc_user');
define('UC_DBPW', 'secure_password');
define('UC_DBNAME', 'ucenter');
define('UC_DBCHARSET', 'utf8');
define('UC_DBTABLEPRE', 'uc_');
define('UC_DBCONNECT', 0); // 1=持久連接
// 通用配置(兩種模式都需要)
define('UC_KEY', 'your_32char_auth_key'); // 必須與服務端 config.inc.php 一致
define('UC_API', 'https://uc.example.com'); // 服務端 URL(遠程模式必填)
define('UC_IP', '10.0.1.10'); // 服務端 IP(DNS 解析失敗時備用)
define('UC_CHARSET', 'utf-8'); // 通信字符集
define('UC_APPID', 2); // 當前應用在 UCenter 中的 ID
所有調用必須先包含客戶端核心文件:
require_once './uc_client/client.php'; // 路徑相對於調用文件
$uid = uc_user_register($username, $password, $email, $questionid, $answer, $regip);
$user = uc_user_login($username, $password, $isuid, $checkques, $questionid, $answer, $ip);
$synlogin = uc_user_synlogin($uid); // 返回 JS 代碼,直接 echo 輸出
$newpm = uc_pm_checknew($uid); // 返回未讀數
uc_pm_send($fromuid, $touids, $subject, $message, $pmtype, $date);
uc_credit_get($uid, $appid); // 獲取積分
uc_credit_update($uid, $amount, $appid); // 增減積分
uc_* 函數返回 false 表示失敗uc_client/client.php 設置 define('UC_DEBUG', true);data/logs/、客戶端 uc_client/data/-1 通信失敗、-2 密鑰錯誤、-3 數據格式錯誤UC_KEY,服務端與所有客戶端同步更新uc_applications.allowips 限制調用來源 IPUC_API 必須使用 HTTPS,防止明文傳輸密鑰/用戶數據UCenter 接口以「常量配置 + client.php 引入 + uc_* 函數調用」的極簡模式,屏蔽了跨應用通信的複雜性。選對連接模式、守護好 UC_KEY、用好調試日誌,即可構建穩固的用戶中心集成層。