integer uc_user_register(string username , string password , string email [, integer questionid , string answer])
函數參數
參數 |
含義 |
string username |
用戶名 |
string password |
密碼 |
string email |
電子郵件 |
integer questionid |
安全提問索引 |
string answer |
安全提問答案 |
string regip |
註冊ip |
返回值
值 |
含義 |
integer |
大於 0:返回用戶 ID,表示用戶註冊成功
-1:用戶名不合法
-2:包含不允許註冊的詞語
-3:用戶名已經存在
-4:Email 格式有誤
-5:Email 不允許註冊
-6:該 Email 已經被註冊
|
本接口函數用於新用戶的註冊。用戶名、密碼、Email 為一個用戶在 UCenter 的基本數據,提交後 UCenter 會按照註冊設置和詞語過濾的規則檢測用戶名和 Email 的格式是否正確合法,如果正確則返回註冊後的用戶 ID,否則返回相應的錯誤信息。
$uid = uc_user_register($_POST['username'], $_POST['password'], $_POST['email']);
if($uid <= 0) {
if($uid == -1) {
echo '用戶名不合法';
} elseif($uid == -2) {
echo '包含要允許註冊的詞語';
} elseif($uid == -3) {
echo '用戶名已經存在';
} elseif($uid == -4) {
echo 'Email 格式有誤';
} elseif($uid == -5) {
echo 'Email 不允許註冊';
} elseif($uid == -6) {
echo '該 Email 已經被註冊';
} else {
echo '未定義';
}
} else {
echo '註冊成功';
}
array uc_user_login(string username , string password [, bool isuid , bool checkques , integer questionid , string answer])
函數參數
參數 |
含義 |
string username |
用戶名 / 用戶 ID / 用戶 E-mail |
string password |
密碼 |
bool isuid |
是否使用用戶 ID登錄
1:使用用戶 ID登錄
2:使用用戶 E-mail登錄
0:(默認值) 使用用戶名登錄
|
bool checkques |
是否驗證安裝提問
1:驗證安全提問
0:(默認值) 不驗證安全提問
|
integer questionid |
安全提問索引 |
string answer |
安全提問答案 |
返回值
值 |
含義 |
array |
integer [0] |
大於 0:返回用戶 ID,表示用戶登錄成功
-1:用戶不存在,或者被刪除
-2:密碼錯
-3:安全提問錯
|
string [1] |
用戶名 |
string [2] |
密碼 |
string [3] |
Email |
bool [4] |
用戶名是否重名 |
本接口函數用於用戶的登錄驗證,用戶名及密碼正確無誤則返回用戶在 UCenter 的基本數據,否則返回相應的錯誤信息。如果應用程序是升級過來的,並且當前登錄用戶和已有用戶重名,那麼返回的數組中 [4] 的值將返回 1。
list($uid, $username, $password, $email) = uc_user_login($_POST['username'], $_POST['password']);
if($uid > 0) {
echo '登錄成功';
} elseif($uid == -1) {
echo '用戶不存在,或者被刪除';
} elseif($uid == -2) {
echo '密碼錯';
} else {
echo '未定義';
}
integer uc_user_edit(string username , string oldpw , string newpw , string email [, bool ignoreoldpw, integer questionid , string answer])
函數參數
參數 |
含義 |
string username |
用戶名 |
string oldpw |
舊密碼 |
string newpw |
新密碼,如不修改為空 |
string email |
Email,如不修改為空 |
bool ignoreoldpw |
是否忽略舊密碼
1:忽略,更改資料不需要驗證密碼
0:(默認值) 不忽略,更改資料需要驗證密碼
|
integer questionid |
安全提問索引 |
string answer |
安全提問答案 |
返回值
值 |
含義 |
integer |
1:更新成功
0:沒有做任何修改
-1:舊密碼不正確
-4:Email 格式有誤
-5:Email 不允許註冊
-6:該 Email 已經被註冊
-7:沒有做任何修改
-8:該用戶受保護無權限更改
|
本接口函數用於更新用戶資料。更新資料需驗證用戶的原密碼是否正確,除非指定 ignoreoldpw 為 1。如果只修改 Email 不修改密碼,可讓 newpw 為空;同理如果只修改密碼不修改 Email,可讓 email 為空。
$ucresult = uc_user_edit($_POST['username'], $_POST['oldpassword'], $_POST['newpassword'], $_POST['emailnew']);
if($ucresult == -1) {
echo '舊密碼不正確';
} elseif($ucresult == -4) {
echo 'Email 格式有誤';
} elseif($ucresult == -5) {
echo 'Email 不允許註冊';
} elseif($ucresult == -6) {
echo '該 Email 已經被註冊';
}
string uc_user_synlogin(integer uid)
函數參數
返回值
值 |
含義 |
string |
同步登錄的 HTML 代碼 |
如果當前應用程序在 UCenter 中設置允許同步登錄,那麼本接口函數會通知其他設置了同步登錄的應用程序登錄,把返回的 HTML 輸出在頁面中即可完成對其它應用程序的通知。輸出的 HTML 中包含執行遠程的 javascript 腳本,請讓頁面在此腳本運行完畢後再進行跳轉操作,否則可能會導致無法同步登錄成功。同時要保證同步登錄的正確有效,請保證其他應用程序的 Cookie 域和 Cookie 路徑設置正確。
list($uid, $username, $password, $email) = uc_user_login($_POST['username'], $_POST['password']);
if($uid > 0) {
echo '登錄成功';
echo uc_user_synlogin($uid);
} elseif($uid == -1) {
echo '用戶不存在,或者被刪除';
} elseif($uid == -2) {
echo '密碼錯';
} else {
echo '未定義';
}
integer uc_user_merge(string/ oldusername , string newusername, integer uid, string password, string email)
函數參數
參數 |
含義 |
string oldusername |
老用戶名 |
string newusername |
新用戶名 |
integer uid |
用戶 ID |
string password |
密碼 |
string email |
電子郵件 |
返回值
值 |
含義 |
integer |
大於 0:返回用戶 ID,表示用戶註冊成功
-1:用戶名不合法
-2:包含不允許註冊的詞語
-3:用戶名已經存在
|
本接口函數用於把重名的用戶合併到 UCenter。
用戶的合併和用戶重名的處理
如果您的應用程序集成到 UCenter 時包含舊的用戶數據,我們建議您可以採取以下範例的方式把您的用戶導入到 UCenter。
function getmaxuid() {
global $ucdb;
$query = $ucdb->query("SHOW CREATE TABLE ".UC_DBTABLEPRE."members");
$data = $ucdb->fetch_array($query);
$data = $data['Create Table'];
if(preg_match('/AUTO_INCREMENT=(\d+?)[\s|$]/i', $data, $a)) {
return $a[1] - 1;
} else {
return 0;
}
}
$maxuid = getmaxuid();
$query = $db->query("SELECT * FROM {$tablepre}members");
while($data = $db->fetch_array($query)) {
$salt = rand(100000, 999999);
$password = md5($data['password'].$salt);
$data['username'] = addslashes($data['username']);
$lastuid = $data['uid'] += $maxuid;
$queryuc = $ucdb->query("SELECT count(*) FROM ".UC_DBTABLEPRE."members WHERE username='$data[username]'");
$userexist = $ucdb->result($queryuc, 0);
if(!$userexist) {
$ucdb->query("INSERT LOW_PRIORITY INTO ".UC_DBTABLEPRE."members SET uid='$data[uid]', username='$data[username]', password='$password',
email='$data[email]', regip='$data[regip]', regdate='$data[regdate]', salt='$salt'", 'SILENT');
$ucdb->query("INSERT LOW_PRIORITY INTO ".UC_DBTABLEPRE."memberfields SET uid='$data[uid]'",'SILENT');
} else {
$ucdb->query("REPLACE INTO ".UC_DBTABLEPRE."mergemembers SET appid='".UC_APPID."', username='$data[username]'", 'SILENT');
}
}
$ucdb->query("ALTER TABLE ".UC_DBTABLEPRE."members AUTO_INCREMENT=".($lastuid + 1));
本方式的基本流程是:首先,獲取當前 UCenter 中的最大用戶 ID 的值。然後,讀取應用程序自己的用戶表,判讀用戶名是否在 UCenter 重複。如果重複,把重名的用戶名保存到 UCenter 的 mergemembers 表中,不合並這個用戶。如果不重名,則按正常方式導入用戶進行合併。當用戶插入到 mergemembers 表後,用戶在這個應用程序登錄的時候,登錄狀態的返回數組中 [4] 的值將返回 1(
請參考上面關於 uc_user_login() 函數的說明)。當登錄狀態返回重名狀態後建議您在應用程序中判讀用戶合法性後進行更名的處理,調用本接口函數。
$uid = uc_user_merge($_GET['username'], $_GET['usernamenew'], $user['uid'], $_GET['password'], $user['email']);
if($uid > 0) {
echo '用戶名可用';
} elseif($uid == -1) {
echo '用戶名不合法';
} elseif($uid == -2) {
echo '包含要允許註冊的詞語';
} elseif($uid == -3) {
echo '用戶名已經存在';
}
$db->query("UPDATE {$tablepre}members SET username='$_GET[usernamenew]' WHERE uid='$uid'");