Home >Backend Development >PHP Tutorial >Single sign-on enables code sharing in Yii2
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('.', $_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 'user' => [ '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. Testing4.1 www.aaa.com login4.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!