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

Single sign-on enables code sharing in Yii2

小云云
Release: 2023-03-21 08:20:01
Original
1309 people have browsed it

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]);
}
Copy after login

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,
],
Copy after login
3. Using

<?php
//设置
Yii::$app->session['var']='value';
//使用
echo Yii::$app->session['var'];
//移除
unset(Yii::$app->session['var']);
Copy after login
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!

Related labels:
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