Home  >  Article  >  Backend Development  >  Yii2 implements single sign-on

Yii2 implements single sign-on

php中世界最好的语言
php中世界最好的语言Original
2018-03-24 16:48:061615browse

This time I will bring you Yii2 to implement single sign-on. What are the precautions for Yii2 to implement single sign-on? The following is a practical case, let’s take a look.

This article introduces the method of implementing single sign-on in Yii2 and shares it with everyone. The details are as follows:

Modify/common/config/main.php

1. Add the following code to the config header

<?php
// Session 跨域
$host = explode(&#39;.&#39;, $_SERVER["HTTP_HOST"]);
if (count($host) > 2) {
  define('DOMAIN', $host[1] . '.' . $host[2]);
} else {
  define('DOMAIN', $host[0] . '.' . $host[1]);
}

2. Add

to the components configuration of config
<?php
&#39;user&#39; => [
  'identityClass' => 'common\models\User',
  'enableAutoLogin' => true,
  'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => '.'.DOMAIN],
],
'session' => [
  'cookieParams' => ['domain' => '.'.DOMAIN, 'lifetime' => 0],
  'timeout' => 3600,
],

3. Use in controller

<?php
//设置
Yii::$app->session['var']='value';
//使用
echo Yii::$app->session['var'];
//移除
unset(Yii::$app->session['var']);

4. Test

4.1 www.aaa.com login

4.2 www.bbb.com session is still effective.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to generate random numbers in PHP

How to use PHP’s recursive function

The above is the detailed content of Yii2 implements single sign-on. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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