1.进入ucenter应用管理,添加应用
2.在yii网站的根目录下添加api和uc_client文件夹和config.inc.php文件
config.inc.php代码如下:
<?php
define('UC_CONNECT', 'mysql');
define('UC_DBHOST', 'localhost');
define('UC_DBUSER', 'root');
define('UC_DBPW', '123456');
define('UC_DBNAME', 'home');
define('UC_DBCHARSET', 'utf8');
define('UC_DBTABLEPRE', 'home.pre_ucenter_'); // UCenter 数据库表前缀
define('UC_KEY', '02ecbnjQt17LIaEpc5 Caolov4zsA1y2ajBBN7U'); // 与 UCenter 的通信密钥
define('UC_API', 'http://localhost/bbs/uc_server'); // UCenter 的 URL 地址, 在调用头像时依赖此常量
define('UC_CHARSET', 'utf-8');
define('UC_IP', '');
define('UC_APPID', '2'); // 当前应用的 ID
注意:这里是ucenter的配置信息。
//ucexample_2.php 用到的应用程序数据库连接参数
$dbhost ='localhost'; //数据库服务器
$dbuser ='root'; //数据库用户名
$dbpw ='123456'; //数据库密码
$dbname ='home'; //数据库名
$pconnect =0; //数据库持久连接 0=关闭, 1=打开
$tablepre = 'Example_'; // 表名前缀, 同一数据库安装多个论坛请修改此处
$dbcharset ='utf8'; //MySQL 字符集, 可选 'gbk', 'big5', 'utf8', 'latin1', 留空为按照论坛字符集设定
//同步登录 Cookie 设置
$cookiedomain = ''; // cookie作用域
$cookiepath ='/'; // cookie作用路径
3调用,写成一个方法:
class MyFormatter extends CFormatter
{
public function checkLoginStatus()
{
include dirname(__FILE__).'/../../config.inc.php';
include dirname(__FILE__).'/../../uc_client/client.php';
if(!empty($_COOKIE['Example_auth'])) {
return list($Example_uid, $Example_username) = explode("\t", uc_authcode($_COOKIE['Example_auth'], 'DECODE'));
} else {
return $Example_uid = $Example_username = '';
}
}
}
页面:$user = MyFormatter::checkLoginStatus();
$user[0]; //用户ID
$user[1]; //用户名
4退出
public function actionLoginout()
{
include dirname(__FILE__).'/../../config.inc.php';
include dirname(__FILE__).'/../../uc_client/client.php';
$out = uc_user_synlogout();
//echo $out; //这个是在页面输出一个js ,实现退出
$this->layout=false;
$this->render('out',array('out'=>$out));
}
yii通过uc实现同步登陆
最新推荐文章于 2021-03-16 14:51:09 发布