本文介绍了Yii2 中实现单点登录的方法,分享给大家,具体如下:
修改 /common/config/main.php
一、在 config 头部上加上以下代码
2) {
define('DOMAIN', $host[1] . '.' . $host[2]);
} else {
define('DOMAIN', $host[0] . '.' . $host[1]);
}二、在 config 的 components 配置中加入
[ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, 'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => '.'.DOMAIN], ], 'session' => [ 'cookieParams' => ['domain' => '.'.DOMAIN, 'lifetime' => 0], 'timeout' => 3600, ],
三、controller 中使用
session['var']='value'; //使用 echo Yii::$app->session['var']; //移除 unset(Yii::$app->session['var']);