Home >Backend Development >PHP Tutorial >Single sign-on enables code sharing in Yii2

Single sign-on enables code sharing in Yii2

小云云
小云云Original
2018-03-12 09:43:241328browse

This article mainly introduces to you the method of implementing single sign-on in Yii2. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

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. Using

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

4.1 www.aaa.com login

4.2 www.bbb.com session still has effect.

Related recommendations:


Single sign-on cookie analysis and implementation in PHP

Single sign-on principle and simple implementation

php implements single sign-on in web system_PHP tutorial

The above is the detailed content of Single sign-on enables code sharing in Yii2. 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