ucenter使用教程
UCenter 的中文意思就是“用户中心”,其中的 U 代表 User 也代表 You ,取其中的含义就是“用户中心”,或者说“你(最终用户)的中心”。 UCenter 是 Comsenz 旗下各个产品之间信息直接传递的一个桥梁,通过 UCenter站长可以无缝整合 Comsenz 系列产品,实现用户的一站式注册、登录、退出以及社区其他数据的交互的php框架。
下载相应的软件
CodeIgniter 2.1.4
UCenter 1.6.0
Discuz! X3
简体 UTF-8,可以不下载 Discuz!,这里只是用来最后检验同步登录/登出是否成功。
前置条件
项目目录结构如下
搭建服务器
根目录指向 codeIgniter-ucenter
我设置了 hosts
127.0.0.1 ci.connect.uc
这个任意的,这里只是为了突出主题。
开始集成
安装 Ucenter
访问http://ci.connect.uc/uc,按照向导安装即可。
安装 Discuz!X
访问http://ci.connect.uc/discuz,按照向导,第二步时候选择只安装 Discuz!X,如下图,信息填写uc的安装信息
配置 CodeIgniter
配置 application/config.php, 为使用 Session 类,需要设置 encryption_key,值任意。
$config['encryption_key'] = sha1('whatever it is!');
修改默认控制器 welcome.php 如下:
load->library('session'); $data['user'] = $this->session->userdata('user'); $this->load->view('welcome_message', $data); } }
修改 views/welcome_message.php 中的div#container如下:
在 UCenter 中添加新应用
在 CI 目录 application/config/ 目录下新建文件 ucenter.php
内容为 UCenter 为新加应用生成的配置文件
从 UCenter 安装包中复制 uc_client 到 CI 根目录
新增控制器 api/uc.php
内容如下:
*/class Uc extends CI_Controller{ const UC_CLIENT_RELEASE = '20110501'; const UC_CLIENT_VERSION = '1.6.0'; const API_DELETEUSER = 1; const API_RENAMEUSER = 1; const API_GETTAG = 1; const API_SYNLOGIN = 1; const API_SYNLOGOUT = 1; const API_UPDATEPW = 1; const API_UPDATEBADWORDS = 1; const API_UPDATEHOSTS = 1; const API_UPDATEAPPS = 1; const API_UPDATECLIENT = 1; const API_UPDATECREDIT = 1; const API_GETCREDITSETTINGS = 1; const API_GETCREDIT = 1; const API_UPDATECREDITSETTINGS = 1; const API_RETURN_SUCCEED = 1; const API_RETURN_FAILED = -1; const API_RETURN_FORBIDDEN = -2; public function index() { include APPPATH.'config/ucenter.php'; $get = $post = array(); $code = $this->input->get('code', true); parse_str(self::authcode($code, 'DECODE', UC_KEY), $get); $timestamp = time(); if ($timestamp - $get['time'] > 3600) { echo '授权已过期'; return; } if (empty($get)) { echo '非法请求'; return; } $post = self::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'))) { echo $this->$get['action']($get, $post); return; } else { echo self::API_RETURN_FAILED; return; } } private function test($get, $post) { return self::API_RETURN_SUCCEED; } private function deleteuser($get, $post) { if ( ! self::API_DELETEUSER) { return self::API_RETURN_FORBIDDEN; } $uids = $get['ids']; //delete your users here return self::API_RETURN_SUCCEED; } private function gettag($get, $post) { if ( ! self::API_GETTAG) { return self::API_RETURN_FORBIDDEN; } // return self::API_RETURN_SUCCEED; } private function synlogin($get, $post) { if ( ! self::API_SYNLOGIN) { return self::API_RETURN_FORBIDDEN; } header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); $uid = $get['uid']; //同步登录的代码在这里处理 include APPPATH.'../uc_client/client.php'; if ($uc_user = uc_get_user($uid, 1)) { $this->load->library('session'); $this->session->set_userdata('user', array( 'uid' => $uid, 'username' => $uc_user )); } return self::API_RETURN_SUCCEED; } private function synlogout($get, $post) { if ( ! self::API_SYNLOGOUT) { return self::API_RETURN_FORBIDDEN; } header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); $this->load->library('session'); $this->session->sess_destroy(); return self::API_RETURN_SUCCEED; } private function updatepw($get, $post) { if ( ! self::API_UPDATEPW) { return self::API_RETURN_FORBIDDEN; } //这里做修改密码操作 return self::API_RETURN_SUCCEED; } private function updatebadwords($get, $post) { if ( ! self::API_UPDATEBADWORDS) { return self::API_RETURN_FORBIDDEN; } $cachefile = APPPATH.'../uc_client/data/cache/badwords.php'; @unlink($cachefile); return self::API_RETURN_SUCCEED; } private function updatehosts($get, $post) { if ( ! self::API_UPDATEHOSTS) { return self::API_RETURN_FORBIDDEN; } $cachefile = APPPATH.'../uc_client/data/cache/hosts.php'; @unlink($cachefile); return self::API_RETURN_SUCCEED; } private function updateapps($get, $post) { if ( ! self::API_UPDATEAPPS) { return self::API_RETURN_FORBIDDEN; } $cachefile = APPPATH.'../uc_client/data/cache/apps.php'; @unlink($cachefile); return self::API_RETURN_SUCCEED; } private function updateclient($get, $post) { if ( ! self::API_UPDATECLIENT) { return self::API_RETURN_FORBIDDEN; } $cachefile = APPPATH.'../uc_client/data/cache/settings.php'; @unlink($cachefile); return self::API_RETURN_SUCCEED; } private function updatecredit($get, $post) { if ( ! self::API_UPDATECREDIT) { return self::API_RETURN_FORBIDDEN; } return self::API_RETURN_SUCCEED; } private function getcredit($get, $post) { if ( ! self::API_GETCREDIT) { return self::API_RETURN_FORBIDDEN; } return self::API_RETURN_SUCCEED; } public static 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)); } } public static function serialize($arr, $htmlOn = 0) { if ( ! function_exists('xml_serialize')) { require APPPATH.'../uc_client/lib/xml.class.php'; } return xml_serialize($arr, $htmlOn); } public static function unserialize($xml, $htmlOn = 0) { if ( ! function_exists('xml_serialize')) { require APPPATH.'../uc_client/lib/xml.class.php'; } return xml_unserialize($xml, $htmlOn); } public static function gbk2utf8($string) { return iconv("GB2312", "UTF-8//IGNORE", $string); } public static function utf82gbk($string) { return iconv("UTF-8", "GB2312//IGNORE", $string); }}
运行
打开http://ci.connect.uc/ci
点击页面链接进行登录操作
刷新即可看到已登录
点击页面链接进行退出操作
再次刷新即可看到已退出
版权声明:本站【趣百科】文章素材来源于网络或者用户投稿,未经许可不得用于商用,如转载保留本文链接:https://www.qubaik.com/life/31888.html