我是ucenter gbk编码 本例子是 utf8编码 如果编码想通可以省略几步。
./config.inc.php 配置文件
./uc_client/client.php; uc核心文件
./db/db_mysql.class.php 数据库文件
登陆用户。
<?php
header("Content-type: text/html; charset=utf-8");
include './config.inc.php';
include './db/db_mysql.class.php';
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
include './uc_client/client.php';
/**
* 获取当前用户的 UID 和 用户名
* Cookie 解密直接用 uc_authcode 函数,也可以本站用户自己的函数
*/
if(!empty($_COOKIE['your_auth'])) {
list($uid, $username) = explode("\t", uc_authcode($_COOKIE['your_auth'], 'DECODE'));
} else {
$uid = $username = '';
}
echo "用户id:".$uid."<br>";
echo "用户名:".$username."<br>";
if(empty($_POST['submit'])) {
//登录表单
echo '<form method="post" action="?login">';
echo '登录:';
echo '<dl><dt>用户名</dt><dd><input name="username"></dd>';
echo '<dt>密码</dt><dd><input name="password" type="password"></dd></dl>';
echo '<input name="submit" type="submit"> ';
echo '</form>';
exit;
}
$username_gbk = mb_convert_encoding($_POST['username'], "gbk", "UTF-8"); //编码不同才需要
list($uid, $username, $password, $email) = uc_user_login($username_gbk, $_POST['password']);
$username = mb_convert_encoding($username, "UTF-8", "gbk"); //编码不同才需要
//设置cookie
setcookie('your_auth', '', -86400);
//如果uc用户表存在用户
if($uid > 0) {
if(!$db->result_first("SELECT count(*) FROM {$tablepre}user WHERE username='{$username}'")) { //hot_user自己的数据库
// 这里也可以比对密码 更新该用户的本站密码
//判断用户是否存在于用户表,不存在则跳转到激活页面或者直接插入自己的数据库
$db->query("INSERT INTO {$tablepre}user (username,password,email,num) VALUES ('{$username}','$password','$email',0)");
}
//这里就是 uc表已经存在用户,并且自己的表也有该用户
//用户登陆成功,设置 Cookie,加密直接用 uc_authcode 函数,或者本网站的加密方式等。。
setcookie('your_auth', uc_authcode($uid."\t".$username, 'ENCODE'));
//生成同步登录的代码
$ucsynlogin = uc_user_synlogin($uid);
echo '登录成功'.$ucsynlogin;
}elseif($uid == -1) {
echo '用户不存在,或者被删除';
}elseif($uid == -2) {
echo '密码错';
}else{
echo '未定义';
}
?>
意思就是说 先读取uc 查看帐号密码是否正确 ,在读取自己的表是否存在该帐号 ,不存在就入库。
2.注册帐号
<?php
header("Content-type: text/html; charset=utf-8");
include './config.inc.php';
include './db/db_mysql.class.php';
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect,$bm);
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
include './uc_client/client.php';
if(empty($_POST['submit'])) {
//注册表单
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?i">';
echo '注册:';
echo '<dl><dt>用户名</dt><dd><input name="username"></dd>';
echo '<dt>密码</dt><dd><input name="password" type="password"></dd></dl>';
echo '<dt>信箱</dt><dd><input name="email" ></dd></dl>';
echo '<input name="submit" type="submit"> ';
echo '</form>';
exit;
}
$username_gbk = mb_convert_encoding($_POST['username'], "gbk", "UTF-8");//编码不同才需要
$uid = uc_user_register($username_gbk, $_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 {
$username = $_POST['username'];
}
if($username) {
if(!$db->result_first("SELECT count(*) FROM {$tablepre}user WHERE username='{$_POST['username']}'")) { //hot_user自己的数据库
//判断用户是否存在于用户表,不存在则跳转到激活页面或者直接插入自己的数据库
// 这里也可以比对本站密码 更新该用户的本站密码
$db->query("INSERT INTO {$tablepre}user (username,password,email,num) VALUES ('{$_POST['username']}','{$_POST['password']}','{$_POST['email']}',0)");
}
//注册成功,设置 Cookie,加密直接用 uc_authcode 函数,用户使用自己的函数
setcookie('your_auth', uc_authcode($uid."\t".$username, 'ENCODE'));
$ucsynlogin = uc_user_synlogin($uid);//并同步登录
echo '注册成功'.$ucsynlogin;
}
?>
首先先去注册UC方便的信息。
当然 自己的这方面 也需要判断 验证等。。。。这个的意思 就是先注册uc去 如果 uc通过了。 你就可以直接安心写入自己的表了。
3.退出。
<?php
header("Content-type: text/html; charset=utf-8");
include './config.inc.php';
include './uc_client/client.php';
setcookie('your_auth', '', -86400);
//生成同步退出的代码
$ucsynlogout = uc_user_synlogout();
echo '退出成功'.$ucsynlogout;
exit;
?>
配置文件
<?php
define('UC_CONNECT', 'mysql'); // 连接 UCenter 的方式: mysql/NULL, 默认为空时为 fscoketopen()
// mysql 是直接连接的数据库, 为了效率, 建议采用 mysql
//数据库相关 (mysql 连接时, 并且没有设置 UC_DBLINK 时, 需要配置以下变量)
define('UC_DBHOST', 'localhost'); // UCenter 数据库主机
define('UC_DBUSER', 'root'); // UCenter 数据库用户名
define('UC_DBPW', '######'); // UCenter 数据库密码
define('UC_DBNAME', 'ucenter'); // UCenter 数据库名称
define('UC_DBCHARSET', 'gbk'); // UCenter 数据库字符集
define('UC_DBTABLEPRE', 'ucenter.uc_'); // UCenter 数据库表前缀
//通信相关
define('UC_KEY', 'key'); // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
define('UC_API', 'http://uc.sina.cn'); // UCenter 的 URL 地址, 在调用头像时依赖此常量
define('UC_CHARSET', 'gbk'); // UCenter 的字符集
define('UC_IP', '8.8.8.8'); // UCenter 的 IP, 当 UC_CONNECT 为非 mysql 方式时, 并且当前应用服务器解析域名有问题时, 请设置此值
define('UC_APPID', 25); // 当前应用的 ID
//自己数据库用到的应用程序数据库连接参数
$dbhost = 'localhost'; // 数据库服务器
$dbuser = 'root'; // 数据库用户名
$dbpw = '@@@@@'; // 数据库密码
$dbname = 'dbname'; // 数据库名
$pconnect = 0; // 数据库持久连接 0=关闭, 1=打开
$tablepre = 'hot_'; // 表名前缀, 同一数据库安装多个论坛请修改此处
$dbcharset = 'utf-8'; // MySQL 字符集, 可选 'gbk', 'big5', 'utf8', 'latin1', 留空为按照论坛字符集设定
//同步登录 Cookie 设置
$cookiedomain = ''; // cookie 作用域
$cookiepath = '/'; // cookie 作用路径
api/uc.php 片段 演示 同步登陆和同步退出
function synlogin($get, $post) {
$uid = $get['uid'];
$username = $get['username'];
if(!API_SYNLOGIN) {
return API_RETURN_FORBIDDEN;
}
$username = mb_convert_encoding($username, "UTF-8", "gbk"); //不同编码才需要转换
if(!$this->db->result_first("SELECT count(*) FROM hot_user WHERE username='{$username}'")) { //hot_user自己的数据库
//如果本站不信息存在 写入数据库
$this->db->query("INSERT INTO hot_user (username,password,email,num) VALUES ('{$username}','00000000000','000@qq.com',0)");
}
else{
$this->db->query("update hot_user set num=num+1 where username='{$username}'");
}
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
_setcookie('your_auth', _authcode($uid."\t".$username, 'ENCODE')); //本站的用户信息写入登录状态
}
function synlogout($get, $post) {
if(!API_SYNLOGOUT) {
return API_RETURN_FORBIDDEN;
}
//note 同步登出 API 接口
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
_setcookie('your_auth', '', -86400 * 365);//和本站的用户信息一起退出
}
----------------------------------------------------------------------------------
完整 uc.php
<?php
define('IN_DISCUZ', TRUE);
define('UC_CLIENT_VERSION', '1.5.0'); //note UCenter 版本标识
define('UC_CLIENT_RELEASE', '20081031');
define('API_DELETEUSER', 1); //note 用户删除 API 接口开关
define('API_RENAMEUSER', 1); //note 用户改名 API 接口开关
define('API_GETTAG', 1); //note 获取标签 API 接口开关
define('API_SYNLOGIN', 1); //note 同步登录 API 接口开关
define('API_SYNLOGOUT', 1); //note 同步登出 API 接口开关
define('API_UPDATEPW', 1); //note 更改用户密码 开关
define('API_UPDATEBADWORDS', 0); //note 更新关键字列表 开关
define('API_UPDATEHOSTS', 0); //note 更新域名解析缓存 开关
define('API_UPDATEAPPS', 0); //note 更新应用列表 开关
define('API_UPDATECLIENT', 1); //note 更新客户端缓存 开关
define('API_UPDATECREDIT', 0); //note 更新用户积分 开关
define('API_GETCREDITSETTINGS', 1); //note 向 UCenter 提供积分设置 开关
define('API_GETCREDIT', 0); //note 获取用户的某项积分 开关
define('API_UPDATECREDITSETTINGS', 0); //note 更新应用积分设置 开关
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '-2');
define('DISCUZ_ROOT', '../');
//note 普通的 http 通知方式
if(!defined('IN_UC')) {
error_reporting(0);
set_magic_quotes_runtime(0);
defined('MAGIC_QUOTES_GPC') || define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
require_once DISCUZ_ROOT.'./config.inc.php';
$_DCACHE = $get = $post = array();
$code = @$_GET['code'];
parse_str(_authcode($code, 'DECODE', UC_KEY), $get);
if(MAGIC_QUOTES_GPC) {
$get = _stripslashes($get);
}
$timestamp = time();
if($timestamp - $get['time'] > 3600) {
exit('Authracation has expiried');
}
if(empty($get)) {
exit('Invalid Request');
}
$action = $get['action'];
require_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
$post = xml_unserialize(file_get_contents('php://input'));
if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcreditsettings', 'updatecreditsettings'))) {
require_once DISCUZ_ROOT.'./db/db_mysql.class.php';
$GLOBALS['db'] = new dbstuff;
$GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
$GLOBALS['tablepre'] = $tablepre;
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
$uc_note = new uc_note();
exit($uc_note->$get['action']($get, $post));
} else {
exit(API_RETURN_FAILED);
}
//note include 通知方式
} else {
require_once DISCUZ_ROOT.'./config.inc.php';
require_once DISCUZ_ROOT.'./db/db_mysql.class.php';
$GLOBALS['db'] = new dbstuff;
$GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
$GLOBALS['tablepre'] = $tablepre;
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
}
class uc_note {
var $dbconfig = '';
var $db = '';
var $tablepre = '';
var $appdir = '';
function _serialize($arr, $htmlon = 0) {
if(!function_exists('xml_serialize')) {
include_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
}
return xml_serialize($arr, $htmlon);
}
function uc_note() {
$this->appdir = substr(dirname(__FILE__), 0, -3);
$this->dbconfig = $this->appdir.'./config.inc.php';
$this->db = $GLOBALS['db'];
$this->tablepre = $GLOBALS['tablepre'];
}
function test($get, $post) {
return API_RETURN_SUCCEED;
}
function deleteuser($get, $post) {
$uids = $get['ids'];
!API_DELETEUSER && exit(API_RETURN_FORBIDDEN);
return API_RETURN_SUCCEED;
}
function renameuser($get, $post) {
$uid = $get['uid'];
$usernameold = $get['oldusername'];
$usernamenew = $get['newusername'];
if(!API_RENAMEUSER) {
return API_RETURN_FORBIDDEN;
}
return API_RETURN_SUCCEED;
}
function gettag($get, $post) {
$name = $get['id'];
if(!API_GETTAG) {
return API_RETURN_FORBIDDEN;
}
$return = array();
return $this->_serialize($return, 1);
}
function synlogin($get, $post) {
$uid = $get['uid'];
$username = $get['username'];
if(!API_SYNLOGIN) {
return API_RETURN_FORBIDDEN;
}
$username = mb_convert_encoding($username, "UTF-8", "gbk");
if(!$this->db->result_first("SELECT count(*) FROM hot_user WHERE username='{$username}'")) { //hot_user自己的数据库
//如果本站不信息存在 写入数据库
$this->db->query("INSERT INTO hot_user (username,password,email,num) VALUES ('{$username}','00000000000','000@qq.com',0)");
}
else{
$this->db->query("update hot_user set num=num+1 where username='{$username}'");
}
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
_setcookie('your_auth', _authcode($uid."\t".$username, 'ENCODE')); //本站的用心信息写入登录状态
}
function synlogout($get, $post) {
if(!API_SYNLOGOUT) {
return API_RETURN_FORBIDDEN;
}
//note 同步登出 API 接口
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
_setcookie('your_auth', '', -86400 * 365);//和本站的用户信息一起退出
}
function updatepw($get, $post) {
if(!API_UPDATEPW) {
return API_RETURN_FORBIDDEN;
}
$username = $get['username'];
$password = $get['password'];
return API_RETURN_SUCCEED;
}
function updatebadwords($get, $post) {
if(!API_UPDATEBADWORDS) {
return API_RETURN_FORBIDDEN;
}
$cachefile = $this->appdir.'./uc_client/data/cache/badwords.php';
$fp = fopen($cachefile, 'w');
$data = array();
if(is_array($post)) {
foreach($post as $k => $v) {
$data['findpattern'][$k] = $v['findpattern'];
$data['replace'][$k] = $v['replacement'];
}
}
$s = "<?php\r\n";
$s .= '$_CACHE[\'badwords\'] = '.var_export($data, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
return API_RETURN_SUCCEED;
}
function updatehosts($get, $post) {
if(!API_UPDATEHOSTS) {
return API_RETURN_FORBIDDEN;
}
$cachefile = $this->appdir.'./uc_client/data/cache/hosts.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'hosts\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
return API_RETURN_SUCCEED;
}
function updateapps($get, $post) {
if(!API_UPDATEAPPS) {
return API_RETURN_FORBIDDEN;
}
$UC_API = $post['UC_API'];
//note 写 app 缓存文件
$cachefile = $this->appdir.'./uc_client/data/cache/apps.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
//note 写配置文件
if(is_writeable($this->appdir.'./config.inc.php')) {
$configfile = trim(file_get_contents($this->appdir.'./config.inc.php'));
$configfile = substr($configfile, -2) == '?>' ? substr($configfile, 0, -2) : $configfile;
$configfile = preg_replace("/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '$UC_API');", $configfile);
if($fp = @fopen($this->appdir.'./config.inc.php', 'w')) {
@fwrite($fp, trim($configfile));
@fclose($fp);
}
}
return API_RETURN_SUCCEED;
}
function updateclient($get, $post) {
if(!API_UPDATECLIENT) {
return API_RETURN_FORBIDDEN;
}
$cachefile = $this->appdir.'./uc_client/data/cache/settings.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'settings\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
return API_RETURN_SUCCEED;
}
function updatecredit($get, $post) {
if(!API_UPDATECREDIT) {
return API_RETURN_FORBIDDEN;
}
$credit = $get['credit'];
$amount = $get['amount'];
$uid = $get['uid'];
return API_RETURN_SUCCEED;
}
function getcredit($get, $post) {
if(!API_GETCREDIT) {
return API_RETURN_FORBIDDEN;
}
}
function getcreditsettings($get, $post) {
if(!API_GETCREDITSETTINGS) {
return API_RETURN_FORBIDDEN;
}
$credits = array();
return $this->_serialize($credits);
}
function updatecreditsettings($get, $post) {
if(!API_UPDATECREDITSETTINGS) {
return API_RETURN_FORBIDDEN;
}
return API_RETURN_SUCCEED;
}
}
//note 使用该函数前需要 require_once $this->appdir.'./config.inc.php';
function _setcookie($var, $value, $life = 0, $prefix = 1) {
global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
setcookie(($prefix ? $cookiepre : '').$var, $value,
$life ? $timestamp + $life : 0, $cookiepath,
$cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}
function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $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;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$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() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
function _stripslashes($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = _stripslashes($val);
}
} else {
$string = stripslashes($string);
}
return $string;
}