Home > Backend Development > PHP Tutorial > UCenter single sign-on/synchronous login/synchronous logout instance_PHP tutorial

UCenter single sign-on/synchronous login/synchronous logout instance_PHP tutorial

WBOY
Release: 2016-07-13 10:48:33
Original
1489 people have browsed it

ucenter I think many friends will use it. It is a member synchronization function. We often integrate several systems or different forums. Let me introduce it to you.

UCenter synchronization process:

The premise is that you need to add applications that require simultaneous login on Ucenter (at least 2 to see the effect), and display: Communication successful

Suppose I add two application websites A and B

1. First, when site A logs in, after the login is successful, the essence of the process is to call the method provided by uc_client to obtain two script codes (sites A and B) from UCenter. These two script codes are to access A and B. The login method in the website's api/uc.php can be used to perform the session and cookie operations required for login.

2. When logging out, the operation is the same as logging in. You need to obtain 2 script codes from UCenter. The purpose is to trigger the logout method in api/uc.php of stations A and B.

Important: After obtaining the two script codes, you must let the browser run in the output, otherwise synchronous login and logout will not be realized.

Suggestion: When developing and debugging, use browser monitoring to check the return output value. I was using chrome F12 and then opened the Network tab.

Detailed examples

The so-called single sign-on is nothing more than several sites sharing a user center to achieve synchronous login and synchronous logout.

Server side: Loog SSO

Customer service: ucenter, to be honest, the commercialization of dz has indeed made PHP develop a lot.

ucenter basic principle:

In fact, it is the user who logs in in the end, but the user will not notice it if ajax is used.

Let’s take a look at the specific procedures with ucenter:

config.php [PHP code]:

The code is as follows Copy code
 代码如下 复制代码

define(‘UC_CONNECT’, ’mysql’); // 连接 UCenter 的方式: mysql/NULL, 默认为空时为fscoketopen()

//数据库相关 (mysql 连接时, 并且没有设置 UC_DBLINK 时, 需要配置以下变量)
define(‘UC_DBHOST’, ’localhost’); // UCenter 数据库主机
define(‘UC_DBUSER’, ’root’); // UCenter 数据库用户名
define(‘UC_DBPW’, ’123′); // UCenter 数据库密码
define(‘UC_DBNAME’, ’ucenter’); // UCenter 数据库名称
define(‘UC_DBCHARSET’, ’utf8′); // UCenter 数据库字符集
define(‘UC_DBTABLEPRE’, ’ucenter.uc_’); // UCenter 数据库表前缀
define(‘UC_KEY’, ’safefewfef’); // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
define(‘UC_API’, ’http://www.bKjia.c0m/uc’);// UCenter 的 URL 地址, 在调用头像时依赖此常量
define(‘UC_CHARSET’, ’utf-8′); // UCenter 的字符集
define(‘UC_IP’, ’127.0.0.1′); // UCenter 的 IP, 当 UC_CONNECT 为非 mysql 方式时, 并且当前应用服务器解析域名有问题时, 请设置此值
define(‘UC_APPID’, ’3′); // 对应到ucenter当前应用的 ID
define(‘UCDOMAIN’,'http://www.bKjia.c0m/’); // 域名设置

//一些 Cookie 设置
$_UC = array();
$_UC["cookiedomain"] = ”; // cookie 作用域
$_UC["cookiepath"] = ’/'; // cookie 作用路径
$_UC["cookiepre"] = ’uc_’; // cookie 前缀
$_UC["cookietime"] = ’31536000′; //cookie 作用时间

define(‘UC_CONNECT’, ’mysql’); // How to connect to UCenter: mysql/NULL, the default is fscoketopen() //Database related (when mysql is connected and UC_DBLINK is not set, the following variables need to be configured)
define(‘UC_DBHOST’, ’localhost’); // UCenter database host
define(‘UC_DBUSER’, ’root’); // UCenter database user name
define(‘UC_DBPW’, ’123′); // UCenter database password
define(‘UC_DBNAME’, ’ucenter’); // UCenter database name
define(‘UC_DBCHARSET’, ’utf8′); // UCenter database character set
define(‘UC_DBTABLEPRE’, ’ucenter.uc_’); // UCenter database table prefix
define(‘UC_KEY’, ’safefewfef’); // The communication key with UCenter must be consistent with UCenter
define(‘UC_API’, ’http://www.bKjia.c0m/uc’);// URL address of UCenter, relying on this constant when calling the avatar
define(‘UC_CHARSET’, ’utf-8′); // UCenter’s character set
define(‘UC_IP’, ’127.0.0.1′); // The IP of UCenter. When UC_CONNECT is in non-mysql mode and the current application server has problems resolving the domain name, please set this value
define(‘UC_APPID’, ’3′); // Corresponds to the ID of ucenter’s current application
define(‘UCDOMAIN’,’http://www.bKjia.c0m/’); // Domain name settings //Some Cookie Settings
$_UC = array();
$_UC["cookiedomain"] = ”; // cookie scope
$_UC["cookiepath"] = ’/’; // Cookie action path
$_UC["cookiepre"] = ’uc_’; // cookie prefix
$_UC["cookietime"] = ’31536000′; //cookie action time

After the configuration file is written, add an application in the ucenter backend. Remember to customize the ‘UC_KEY’ which must be the same as in config.php

The next step is api/uc.php in your home directory

For example, if the application url is filled in as http://www.bKjia.c0m, then I will have the corresponding http://www.bKjia.c0m/api/uc.php

If you want to customize, please confirm your corresponding relationship.

The most important thing is api/uc.php. Synchronous login is to access uc.php of each application. Dz has already given a demo for this

[PHP code]:

 代码如下 复制代码
define(‘API_DELETEUSER’,0); //note 用户删除 API 接口开关
define(‘API_RENAMEUSER’, 0); //note 用户改名 API 接口开关
define(‘API_GETTAG’, 0); //note 获取标签 API 接口开关
define(‘API_SYNLOGIN’, 1); //note 同步登录 API 接口开关
define(‘API_SYNLOGOUT’, 1); //note 同步登出 API 接口开关
define(‘API_UPDATEPW’, 0); //note 更改用户密码 开关
define(‘API_UPDATEBADWORDS’, 0); //note 更新关键字列表 开关
define(‘API_UPDATEHOSTS’, 0); //note 更新域名解析缓存 开关
define(‘API_UPDATEAPPS’, 0); //note 更新应用列表 开关
define(‘API_UPDATECLIENT’, 0); //note 更新客户端缓存 开关
define(‘API_UPDATECREDIT’, 0); //note 更新用户积分 开关
define(‘API_GETCREDITSETTINGS’, 0); //note 向 UCenter 提供积分设置 开关
define(‘API_GETCREDIT’,0); //note 获取用户的某项积分 开关
define(‘API_UPDATECREDITSETTINGS’, 0); //note 更新应用积分设置 开关

These parameters are function switches provided to other applications

Finally, how to log in to other applications simultaneously with your own page

[PHP code]:

Only using PHP, Kangsheng’s solution is quite good, and it uses the p3p header to achieve single sign-on for different domain names
The code is as follows
 代码如下 复制代码

include_once ’../config.php’;
include_once ’../uc_client/client.php’;

Copy code

 代码如下 复制代码
list($uid, $username, $password) = uc_user_login($_POST[username], $_POST[password]);//进入ucenter验证
$ucsynlogin = uc_user_synlogin($uid);//同步登录
echo $ucsynlogin;//因为是ajax 要echo
include_once ’../config.php’;
include_once ’../uc_client/client.php’;

Your verification login section

The disadvantage is that using ajax client request, if there are more than 10 applications, the login speed will slow down. At this time, you can consider Qiye’s Loong SSO

After understanding the above things, it is not difficult to communicate between PHP’s CMS and ucenter. http://www.bkjia.com/PHPjc/632783.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632783.html
TechArticle
ucenter I think many friends will use it. It is a member synchronization function. We often combine several System or different forums are integrated, let me introduce it to you. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template