当前位置:首页 > PHP教程 > PHP常见问题

Yii2 中实现单点登录的方法

本文介绍了yii2 中实现单点登录的方法,分享给大家,具体如下:

修改 /common/config/main.php

一、在 config 头部上加上以下代码

<?php
// session 跨域
$host = explode('.', $_server["http_host"]);
if (count($host) > 2) {
  define('domain', $host[1] . '.' . $host[2]);
} else {
  define('domain', $host[0] . '.' . $host[1]);
}

二、在 config 的 components 配置中加入

<?php
'user' => [
  'identityclass' => 'commonmodelsuser',
  'enableautologin' => true,
  'identitycookie' => ['name' => '_identity', 'httponly' => true, 'domain' => '.'.domain],
],
'session' => [
  'cookieparams' => ['domain' => '.'.domain, 'lifetime' => 0],
  'timeout' => 3600,
],

三、controller 中使用

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

四、测试

4.1 www.aaa.com 登陆

4.2 www.bbb.com session 依然有效果。 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!